Page 1 of 1

Adding to a vector

Posted: Wed Dec 28, 2016 2:13 pm
by Gangrenous
I understand the most efficient way of adding to a vector is with a push_back. What if I want to add to the beginning of a vector? I should be able to do this with insert from what I read. If I do this, even with it locked, I get a nice error a few seconds later. Specifically I want to add to movement_loop. I have been reading about vectors and somewhat understand the workings, but not completely. Any idea what I cold be doing wrong here?

The exact error is...

Code: Select all

*** Error in `/home/eq2/src/source/WorldServer/eq2world': double free or corruption (fasttop): 0x00007fffa859a3a0 ***

Program received signal SIGABRT, Aborted.
[Switching to Thread 0x7fffd27fc700 (LWP 16940)]
0x00007ffff6497067 in raise () from /lib/x86_64-linux-gnu/libc.so.6

Re: Adding to a vector

Posted: Wed Dec 28, 2016 3:39 pm
by Gangrenous
Okay, I think I have it working but I am not certain why this does not work.

Code: Select all

	MovementData* data = new MovementData;
	data->x = x;
	data->y = y;
	data->z = z;
	data->speed = speed;
	data->delay = delay*1000;
	if(lua_function)
		data->lua_function = string(lua_function);
	
	movement_loop.insert(movement_loop.begin(), data);
	movement_loop.push_back(data);
This crashes. But if I define a new MovementData as data2 and assign everything, then do a push_back on data2 and not data, no crashes.