Archived

This topic is now archived and is closed to further replies.

Clueless about memory leaks :(

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

My program is a game that has to do lots of file reading and saving, and displaying texts. I use char*''s and strings. I need char buffers for the reading of files, and for many functions where it''s easier to use char*''s than strings. But there''s a problem: when a certain kind of text has to be displayed, the program crashes, but the problem is that the problem is outside of that part of the program! totally unrelated things have an influence on it, and what''s worse is that it crashes for some people but not for others! For example: if I use
quote:
char * buffer buffer = new char[size] BLAHBLAH delete[] buffer
in code to load images, it doesn''t crash, but if I use
quote:
char buffer[size]
it crahses! But it doesn''t crash during the image loading, but much later in the game if it has to display a certain text. The scary thing, however, is that in the part to load user profiles, it works if I use
quote:
char buffer[size]
and it crashes if I use "new" and "delete[]", again at that part where it has to display the text. Also, when I say it doesn''t crash, I mean it doesn''t crash on MY computer, for other people it always crashes. The question I''d like to ask is, how do I even begin searching the problem behind this all?

Share this post


Link to post
Share on other sites
How are you using the buffer?

The one allocated statically(char buffer[size]) will be automatically freed when the function it is declared in exits, do not use such variables for passing around. Dynamic memory is much better for that(char * buffer = new char[size]).

Share this post


Link to post
Share on other sites
Leffe is correct, you only use ''delete'' when you have allocated memory using ''new''. But using delete on a pointer that hasn''t been allocated with new will cause your program to crash.


bangz.

http://www.bangz.co.uk

Share this post


Link to post
Share on other sites
I only use delete after I used new, that should've been clear by my quotes.


If my code doesn't contain the words delete and new, there are still weird crashes. That was just an example I gave about how unrelated things make other things crash

Any tips about how to find out the source of such a problem?

[edited by - Boops on March 21, 2004 3:34:49 PM]

Share this post


Link to post
Share on other sites