Program crashing

Started by
22 comments, last by Ruudje 20 years, 4 months ago
quote:Original post by AndyL
quote:Original post by Ruudje
when using an array you always have to use []

Hmmmm... My compiler must realise how cool I am and fix it for me, then.

quote: but could we plz go back to my problem now?

Is it possible that some other part of your program holds a pointer to the data structure that''s being deleted and then reallocated?
For instance if you had a "renderer" object with a pointer to some vertexes, it would crash during the next frame.

(About your object parser, you''re intentionaly throwing away the first token, right? And the LAST token on each line is .ID?)

Two other ideas occured to me that are probably not important at all unless you''re multi-threading, but I thought they were worth mentioning.
1) How would your program react if your renderer were called in the middle of this load operation?
2) strtok(...) isn''t thread-safe.

(P.S. Don''t forget to glDeleteLists.)

-Andy



Well, it does, but any object deleted is re-allocated a few lines later...
Advertisement
quote:Original post by Anonymous Poster
quote:Hmmmm... My compiler must realise how cool I am and fix it for me, then.


Your compiler may be fixing it, but you can not rely on the compiler to do these things. You are probably just getting lucky with you compiler/machine set up. If you ran your code on enough computers, it most likely will cause problems on one of them. Calling delete on an item that was created with new[] is an UNDEFINED operation. That does not mean you can''t go right ahead and do it, it just means that you are can''t be sure what will happen. If you just use delete[] like you should, then you won''t have to worry.


MSVC 6.0 at least, calls the same allocatin/deallocation routine no matter which version of new/delete you use.

You can do:
char *tmp = new char;
delete[] tmp;

char *tmp = new char[10];
delete tmp;

Both compile identically and work without issue, no matter how many machines you run it on, since it''s a compile time thing.

That said, I am not sure which compilers do this, and it''s a good habbit to do it correctly. Whenever you allocate an array, you should delete an array.

char *tmp = new char[10];
delete[] tmp;

And whenever you allocate a variable, you should delete a variable:
char *tmp = new char;
delete tmp;
quote:Well, it does, but any object deleted is re-allocated a few lines later...

Does what? If you have pointers into the area you''re deallocating and reallocating and you''re not updating the pointers this could be your problem. When you allocate the memory again it''s not guarantied to go to the same place...



Ready4Dis : Thanks. That explains it.
well there is 1 pointer, and it points to a 3ds model loaded in. In addition there is 1 gluint pointing to a displaylist of that same model.

The model gets deleted through the global pointer.
The model then gets reloaded and the global pointer now points to the new model.
A new displaylist is formed using the new model.

Then again, I have no idea whats causing it, so it may not even be in the world-model stuff.

This topic is closed to new replies.

Advertisement