Jump to content
  • Advertisement
Sign in to follow this  
Interesting Dave

memory loss, leak or something?

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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.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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!