Sign in to follow this  

Adding one useless line fixes a crash?

This topic is 4277 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

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!

Share this post


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

Share this post


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

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites
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 :)

Share this post


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

Share this post


Link to post
Share on other sites

This topic is 4277 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this