memory loss, leak or something?

This topic is 4901 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Hi people, I seem to be having troubles with my coding, compiler, memory or something, because it keeps forgetting values and reseting them to 0 For example: player_state *ps = &sv.clients[n].ps; vec2 *speed = &ps->speed; (where player_state contains all game related data on a client, and vec2 just has x and y) The values speed->x and speed->y are fine at that point, but a couple of lines later speed->x is reset to 0, as well as ps->acc. Theres nothing inbetween that should be making a difference, and this isnt the only place that my pc seemingly losses interest in what I want it to remember. It was working fine, till I changed something.. but that was in a different area of the game so not exactly sure when the problem started. Am I just overlooking something here, or is something up? cheers

Share on other sites
if the "different area of the game" still has access to that data, it is suspect. Are there any "if (ps->x = 0.0)" type statements? (note the "=" instead of the "==")

Share on other sites
Post the entire function this is occurring in, as well as the definition of sv and the code you use to initialize sv.clients.

Share on other sites
If sv.clients is a std::vector, and the size of the vector changes, its contents can get reassigned to a different memory block, thereby invalidating your pointers. But yeah, post the definition and code and we'll have a better idea.

Share on other sites
The problem isnt just specific to this function, I did have some heat problems earilier..

The function (a lame attempt for a little touch of physics).
(guessing the tag to enter code..)
void SV_MovePlayer(int n, int x, int y){	player_state *ps = &sv.clients[n].ps;	vec2 *speed = &ps->speed;	vec2 intersec, endintersec;	float maxspeed = ps->maxSpeed;	if (x != 0)	{		if (x == 1)		{			speed->x += ps->acc * P_SPEED * sv.frameTime;					if (speed->x > maxspeed)				speed->x = maxspeed;		}		if (x == -1)		{			speed->x -= ps->acc * P_SPEED * sv.frameTime;			if (speed->x < -maxspeed)				speed->x = -maxspeed;		}	}	else	{		if (speed->x - ps->dec > 0)			speed->x -= ps->dec * P_SPEED * sv.frameTime;		else if (speed->x + ps->dec < 0)			speed->x += ps->dec * P_SPEED * sv.frameTime;		else			speed->x = 0;	}	if (y != 0)	{		if (y == 1)		{			speed->y += ps->acc * P_SPEED * sv.frameTime;						if (speed->y > maxspeed)				speed->y = maxspeed;		}		if (y == -1)		{			speed->y -= ps->acc * P_SPEED * sv.frameTime;						if (speed->y < -maxspeed)				speed->y = -maxspeed;		}	}	else	{		if (speed->y - ps->dec > 0)			speed->y -= ps->dec * P_SPEED * sv.frameTime;		else if (speed->y + ps->dec < 0)			speed->y += ps->dec * P_SPEED * sv.frameTime;		else			speed->y = 0;	}	//stop them moving off the playing field.	if(ps->pos.x + speed->x < 0 || ps->pos.x + ps->dim.x + speed->x > map.dim.x)		speed->x = 0;	if(ps->pos.y + speed->y < 0 || ps->pos.y + ps->dim.y + speed->y > map.dim.y)		speed->y = 0;	ps->pos.x += speed->x * sv.frameTime;	ps->pos.y += speed->y * sv.frameTime;	vec2 *mid = &ps->mid;	mid->x = ps->pos.x + ps->dim.x/2;	mid->y = ps->pos.y + ps->dim.y/2;	if(ps->turnType == TURN_MOUSE)		ps->angle = (float)(M_AngleVec2(ps->mid, sv.clients[n].input.xhair) - (3.1415/2));}

definition of sv
typedef struct{	sv_object	objects[MAX_OBJECTS];	sv_client	clients[MAX_CLIENTS];	prop		props[MAX_PROPS];	team		teams[MAX_TEAMS];	float		time;	float		frameTime;	float		lastFrame;	int		clientCount;	int		frame;	int		lastActiveObj;}sv_local;

Share on other sites
oh, and to init sv.clients (called when a player connects)
void SV_BuildClient(){	int n = SV_NextClientSlot();	sv_client *client = &sv.clients[n];	client->obj = &sv.objects[n];	client->index = n;	client->ps.clientnum = n;	client->ps.speed = M_NewVec2(0,0);	client->ps.acc = 1;	client->ps.dec = 5;	client->ps.dim.x = 24;	client->ps.dim.y = 24;	for(int i = 0; i < MAX_INV_SLOTS; i++)		client->ps.inv[i].empty = true;	client->ps.health = 100;	client->ps.maxHealth = 100;	client->ps.maxSpeed = 10;	client->ps.respawnTime = 0;	client->obj->frame = FRAME_PLAYER;	client->ps.turnType = TURN_MOUSE;	client->active = true;}

and when the server is started:
void SV_InitClients(){	for(int n = 0; n < MAX_CLIENTS; n++)	{		sv.clients[n].active = false;		sv.objects[n].team = 0;	}}

Share on other sites
ok, the values seem to being reset after the first frame, which probably means there somewhere in my code thats reseting values (both client and server side)... so off i go to have a looksie :/

Share on other sites
Keep in mind that you can use the debugger to place a watchpoint, that will pause the program when a specific memory location is changed. That could be very useful here.

Share on other sites
Slightly offtopic, I know gdb can do conditional breakpoints and watch breaks like you describe, I've yet to figure out how to do that in VS6... ?

Share on other sites
Debug->New Breakpoint... Select the "Data" tab, enter the variable you want to watch. (to watch the contents of a pointer ptr, use *ptr.) Linky. Note that this is for VS.NET; I'm not sure if 6 does it.

Share on other sites
You wouldn't have multiple threads running would you?

Share on other sites
For VS 6 its edit->breakpoints (or alt+f9)

Share on other sites
If you're single-threaded, you should be able to find it easily by breaking when that variable is written-to. Heck, stepping through line-by-line you should be able to see when it happens. You have lines in your code above that set speed->x and ->y to 0, though I don't see anything that would change acc in that code.

Also try a clean rebuild. It's possible that your dependencies are hosed and something's not recompiling, linking-in old code.

Share on other sites
I am having similar problems. I am working on a chess engine, and keep getting an error in xmemory.

_STD_BEGIN		// TEMPLATE FUNCTION _Allocatetemplate<class _Ty> inline	_Ty _FARQ *_Allocate(_SIZT _Count, _Ty _FARQ *)	{	// allocate storage for _Count elements of type _Ty	return ((_Ty _FARQ *)operator new(_Count * sizeof (_Ty)));	}

I it works when I search 3 moves deep, but anything more than that, and it has a run time error. I think it is blowing the stack, though I can't figure out how. Do you think increasing the stack size will help? if so, how?

Share on other sites
<offtopic>
To change the stack size in MSVC 6, goto project settings (Alt+F7), goto the "Link" tab, select "Output" from the combo box and change the "Stack Allocations -> Reserve value". Its probably similar in other versions of MSVC. The default value is 1MB (1048576 bytes)

What exactly is the assertion you get? If you break to the debugger at that point, what is the value of _Count? Is it stupidly large?
</offtopic>

As for the main problem - Seems like stack corruption, or multithreaded goodness. I don't have anything to say over what the others have said.

Share on other sites
Thanks for the help people, its sorted now. There was actually 2 problems with my code, that effected different parts, that produced the same kind of symptoms

The first, (which caused the problem with code I posted above), SV_BuildClient() wasnt being called correctly.

The second, was for clearing the users input so it wasnt repeated each frame:
for(n = 0; n < MAX_BUTTONS; n++)
cl.input.buttons[n] = 0;

MAX_BUTTONS (= 16) should have been MAX_INPUT (= 5).