Adding one useless line fixes a crash?

Started by
6 comments, last by Daaark 18 years ago
for (count = 0; count < MAX_PLANET_TYPES; count++){
	if (PlanetStructures[count] != NULL){
		MessageBox(NULL,"test",APP_NAME,MB_OK);
		delete PlanetStructures[count];
	}
	if (PlanetImages[count] != NULL){
		DeletePNG(PlanetImages[count]);
		delete PlanetImages[count];
	}
}
Works perfectly, while:
for (count = 0; count < MAX_PLANET_TYPES; count++){
	if (PlanetStructures[count] != NULL){
		delete PlanetStructures[count];
	}
	if (PlanetImages[count] != NULL){
		DeletePNG(PlanetImages[count]);
		delete PlanetImages[count];
	}
}
Causes the program to crash, and give me an option to send an error report. And the worst thing is that the messagebox doesn't even show up, because I'm not loading any structures yet! The inclusion of the messagebox which isn't even accessed suddenly causes the program to run like it's perfect. Why!?! I even tried just adding a semi-colon in place of the messagebox, and it doesn't work!
Advertisement
You are most likely writing outside the bounds of an array. When you add the message box you change the structure of your program sufficently that this is harmless, but without the message box you overwrite some crucial memory area and crash the program. I'd look very carefully at all your memory accesses.

But... that section of the program is not currently being accessed? Would a fault that could happen trigger the error, even though the delete is never actually called?
PlanetStructures[count] is indeed being accessed to do the comparison in the if statement. Are you certain PlanetStructures has a number of elements equal or greater than MAX_PLANET_TYPES when this if statement is reached? Try making the array bigger by +1 and see if that works.
As Erondial said, you're going out of bounds someplace, it's just breaking on the line if (PlanetStructures[count] != NULL) because you're doing a comparison. Check to see if the crash happens when count = MAX_PLANET_TYPES, if it is, you're going over. Otherwise something might be overwriting something else where.

Best of luck, debugger should give you a lot of help finding the issue.

"Those who would give up essential liberty to purchase a little temporary safety deserve neither liberty nor safety." --Benjamin Franklin

Got it. It actually had to do with the bottom half instead of the top... it was a weird way I had written the loading code. But yes, essentially I was writing off the end of an array :) Thanks guys :)
Arrays are evil, vectors almost always do the job better, especially when dealing with loading / managing resources.


Winterdyne Solutions Ltd is recruiting - this thread for details!
if (PlanetImages[count] != NULL), and you delete it, you aren't setting it to NULL for it not to be deleted the next time. delete doesn't do this automatically.

PlanetImages[count] = NULL;

Also, you can just nuke the whole thing? delete [] PlanetImages;

This topic is closed to new replies.

Advertisement