the walls of sanity are crashing down upon me.

Started by
4 comments, last by TwistedMatrix 21 years, 5 months ago
I''m writing my program, everything is going good. Got my translucent blitting objects going on with pixel collisions. Got my loading routines all working good. Everything looks perfect. Now all the sudden it is crashing all the time! Whats worse is, the crashes are inconsistent. It is pissing me off big time! I have backed up my code and I am sitting here cutting huge chunks out of it to try and isolate the bug but nothing is working! Im on the verge of having to restart my project from scratch and I cant even figure out why!!! Arrghh! - Twisted Matrix
- Twisted Matrix
Advertisement
quote:Original post by TwistedMatrix
Now all the sudden it is crashing all the time! Whats worse is, the crashes are inconsistent.

That probably means you are doing something with memory that doesn''t belong to you. Look out for things like this:

- overrunning array bounds;
- deleting or freeing memory more than once;
- dereferencing wild pointers;
- dereferencing null pointers.
quote:
Im on the verge of having to restart my project from scratch and I cant even figure out why!!! Arrghh!

Let that be a lesson to you to develop incrementally, take back-ups and write automated tests that tell you when something breaks.
i think your write. I know this is some kind of pointer problem but I can't find it. I've done my best to isolate some consistent results in this function...
/*  process_lemming_actions() *  cur is the current lemming, max is the lemming count */void process_lemming_actions(struct lemming *lemming, int cur, int max){   int i,d;//,x,y;   //struct lemming c;      if (lemming[cur].action==WALKING) {     /* check the current lemming for collisions against blockers */     for (i=0;i< max;i++) {       if (lemming.action== BLOCKING) {         if ((lemming.x < lemming[cur].x+ SPRITE_W) &&<br>             (lemming.y < lemming[cur].y+ SPRITE_H) &&<br>             (lemming.x+SPRITE_W >= lemming[cur].x) &&<br>             (lemming.y+SPRITE_H >= lemming[cur].y)) {<br>           /* if they are colliding, then change direction */<br>           d=~lemming[cur].direction;<br>           lemming[cur].direction= d;<br>           /* and push the current lemming out of the collision<br>              (so they dont endlessly turn left and right in place) */<br>           d=lemming.x;<br>           if (lemming[cur].x> d) { lemming[cur].x= d+SPRITE_W; }<br>           else                   { lemming[cur].x= d-SPRITE_W; }<br>         }<br>       }<br>     }<br>     move_lemming(&lemming[cur]);<br>   }<br><br>   /* if a lemming is a "digger" then start choping away pixels */<br>   for (i=0;i< max;i++) {<br>     if (lemming.action==DIGGING) {<br>//       d=lemming.x;<br>//       a=lemming.y;a+=SPRITE_H;<br>//       for (x=d-1; x<d+SPRITE_W; x++) {<br>//         putpixel(scene,x,a, mask);<br>//       }<br>//       d=lemming.x+rnd(4);<br>//       for (x=d-1; x<d+SPRITE_W-4; x++) {<br>//         putpixel(scene,x,lemming.y+SPRITE_H+1, mask);<br>//       }<br>//       d=lemming.x+rnd(8);<br>//       for (x=d-1; x<d+SPRITE_W-8; x++) {<br>//         putpixel(scene,x,lemming.y+SPRITE_H+2, mask);<br>//       }<br><br>     }<br>   }<br>}<br>END_OF_FUNCTION(process_lemming_actions);<br>  </pre>  <br><br>as you can see, most of it is still cut out. <img src="smile.gif" width=15 height=15 align=middle><br>as the function sits, it works correctly every time I run it. however, changle the first line to:  int i,d,x,y;<br>and It malfunctions. additionally, if I Un-remark the second line, it begins to work again.<br><br>somthing really weird is going &#111;n because these added variables do absolutly nothing. They are not used anywhere else in the program.   </i>    <br><br>- Twisted Matrix<br><br><SPAN CLASS=editedby>[edited by - TwistedMatrix &#111;n November 20, 2002 12:23:26 PM]</SPAN>    
- Twisted Matrix
This line:

if (lemming.action== BLOCKING) {

needs to be fixed. Should most likely be lemming .action.<br><br>Also,<br><br>d=~lemming[cur].direction;<br><br>Are you sure you want a bitwise not &#111;n there?<br><br>If lemming[cur].direction = 1 then d will probably equal something like 65534.<br><br>Perhaps change that to lemming[cur].direction *= -1;<br><br><SPAN CLASS=editedby>[edited by - Melraidin &#111;n November 20, 2002 12:33:53 PM]</SPAN>
for some reasont the forum cut it out. the line is actually

if (lemming.action== BLOCKING) {<br><br>and also, yes the "Binary NOT" was intentional. Im still a newbie, so if you have any other ideas for doing a quick XOR type effect &#111;n (eg &#111;n; off; &#111;n; off, ect)<br><br>this is driving me crazy. <img src="sad.gif" width=15 height=15 align=middle>
- Twisted Matrix
There is a better solution to your if''s wich produces smaller code. Programmer_One told me this solution once. I guess all your actions are #defined or constants. Why don''t you use an switch to check this? This will produce waay faster and smaller code:


  // Made them 1000 or higher to make it look cool#define MOVING 1000#define BLOCKING 1001#define CHOPPING 1002#define WHATEVER 1003// Do Lemming shite herefor (iIndex = 0; iIndex < MAX_LEMS; iIndex++){   switch (Lemming[iIndex])   {      case MOVING: Move_Lemming(); break;      case BLOCKING: Block_Lemming(); break;      case CHOPPING: Chop_Lemming(); break;      case WHATEVER: Whatever(); break;      default: break; // Should shouldn''t come here   } // END switch} // END for  


This solution will be easier to read, and will be much smaller and so, it will be easier to debug since you don''t have large pieces of code where you could make a mistake.

Sand Hawk

----------------
-Earth is 98% full. Please delete anybody you can.


My Site
----------------(Inspired by Pouya)

This topic is closed to new replies.

Advertisement