Forum

WTF of the day

Discuss programming topics for the various GPL'd game engine sources.

Moderator: InsideQC Admins

WTF of the day

Postby metlslime » Wed Jan 14, 2009 11:10 am

I've spent about three nights in a row trying to figure out how i'm corrupting memory in the tempentity list... even when i remove the code i added, it still crashes spectacularly. On the other hand, sometimes it works fine and i figured it must be a compiler bug.

Until just now, when i discovered something peculiar in CL_UpdateTEnts -- there is a for loop inside another for loop, and both loops use the variable "i" as their iterator. Anyway, here's the code (100% pure id software code), see for yourself:

Code: Select all
void CL_UpdateTEnts (void)
{
   int         i;
   beam_t      *b;
   vec3_t      dist, org;
   float      d;
   entity_t   *ent;
   float      yaw, pitch;
   float      forward;

   num_temp_entities = 0;

// update lightning
   for (i=0, b=cl_beams ; i< MAX_BEAMS ; i++, b++)
   {
      if (!b->model || b->endtime < cl.time)
         continue;

   // if coming from the player, update the start position
      if (b->entity == cl.viewentity)
      {
         VectorCopy (cl_entities[cl.viewentity].origin, b->start);
      }

   // calculate pitch and yaw
      VectorSubtract (b->end, b->start, dist);

      if (dist[1] == 0 && dist[0] == 0)
      {
         yaw = 0;
         if (dist[2] > 0)
            pitch = 90;
         else
            pitch = 270;
      }
      else
      {
         yaw = (int) (atan2(dist[1], dist[0]) * 180 / M_PI);
         if (yaw < 0)
            yaw += 360;
   
         forward = sqrt (dist[0]*dist[0] + dist[1]*dist[1]);
         pitch = (int) (atan2(dist[2], forward) * 180 / M_PI);
         if (pitch < 0)
            pitch += 360;
      }

   // add new entities for the lightning
      VectorCopy (b->start, org);
      d = VectorNormalize(dist);
      while (d > 0)
      {
         ent = CL_NewTempEntity ();
         if (!ent)
            return;
         VectorCopy (org, ent->origin);
         ent->model = b->model;
         ent->angles[0] = pitch;
         ent->angles[1] = yaw;
         ent->angles[2] = rand()%360;

         for (i=0 ; i<3 ; i++)
            org[i] += dist[i]*30;
         d -= 30;
      }
   }
}


My guess is that the memory immediately after cl_beams[] is usually full of zeros, and that's why it usually works.
metlslime
 
Posts: 316
Joined: Tue Feb 05, 2008 11:03 pm

Postby mh » Wed Jan 14, 2009 11:28 am

Aaaaaaaaahhhhhhhhhhh. :D

I had the very same last week (made worse by the fact that I no longer had any memory after cl_beams...) and kinda hacked around it by doing a check for "if (cl.time < 0.0001) return;" at the start, as it only seemed to happen at a changelevel. I was never really happy with that solution, so I'm looking forward to trying this out and seeing if it resolves anything.

Thankingyew!
We had the power, we had the space, we had a sense of time and place
We knew the words, we knew the score, we knew what we were fighting for
User avatar
mh
 
Posts: 2292
Joined: Sat Jan 12, 2008 1:38 am

Postby Spike » Wed Jan 14, 2009 5:22 pm

I didn't realise that was in the original code.
That one caught me out too - I thought it was my own bug!
Spike
 
Posts: 2892
Joined: Fri Nov 05, 2004 3:12 am
Location: UK

Postby mh » Wed Jan 14, 2009 6:12 pm

Yup, that sorted it. :D

Nice one and cheers again for sharing the info. :D :D :D
We had the power, we had the space, we had a sense of time and place
We knew the words, we knew the score, we knew what we were fighting for
User avatar
mh
 
Posts: 2292
Joined: Sat Jan 12, 2008 1:38 am

Postby revelator » Wed Jan 28, 2009 10:27 pm

yep had same.

using the same var in two for loops works in old mode c++ where

for could be declared like this for (int i; i<something;i++) {rest of stuff where the int i is conciled}

then another for (int i; i = something; i--) {stuff here doesnt see the above int}

but i discovered later compilers dont like this method at all and prefers seperate descriptors.

for an example try compiling the blood2 client code with msvc 2005 it will bitch like hell and refuse to link ;)
User avatar
revelator
 
Posts: 2567
Joined: Thu Jan 24, 2008 12:04 pm
Location: inside tha debugger


Return to Engine Programming

Who is online

Users browsing this forum: No registered users and 1 guest