Jump to content
  • Advertisement
Sign in to follow this  
Raeldor

String class and memory leaks

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

I have an array of a particular structure, the structure has strings in it. The array must be a fixed length, so I assume the memory for the strings is allocated on the fly. How does it get released, because I think I am getting memory not released at the end of my program, and it only seems to happen when I put values in these strings. Do the strings need to be released? Thanks

Share this post


Link to post
Share on other sites
Advertisement
Hmm, from experimenting it looks like the memory is released when the owner is released. Is this right? How does the runtime keep track of this? This means I should dynamically allocate the array and free it to make sure the string objects in the structure get released?

Share this post


Link to post
Share on other sites
Er, no?

std::strings will nicely release anything they hold when they're destroyed, which should be when the struct dies.

If you're using char *s in the struct, then yes, you should free them [in the structure's destructor for c++]

And even if you don't, on any modern PC OS, the memory will be reclaimed by the OS at termination.

Code would be helpful.

Share this post


Link to post
Share on other sites
Quote:
Original post by Telastyn
Er, no?

std::strings will nicely release anything they hold when they're destroyed, which should be when the struct dies.

If you're using char *s in the struct, then yes, you should free them [in the structure's destructor for c++]

And even if you don't, on any modern PC OS, the memory will be reclaimed by the OS at termination.

Code would be helpful.


I think he's talking about Java, but it's kind of hard to tell.

Share this post


Link to post
Share on other sites
Quote:
Original post by Roboguy
Quote:
Original post by Telastyn
Er, no?

std::strings will nicely release anything they hold when they're destroyed, which should be when the struct dies.

If you're using char *s in the struct, then yes, you should free them [in the structure's destructor for c++]

And even if you don't, on any modern PC OS, the memory will be reclaimed by the OS at termination.

Code would be helpful.


I think he's talking about Java, but it's kind of hard to tell.


I'd thought of that, a little too late. Though if it were java, wouldn't java do it's automagical garbage collection on it? Anyways, no matter the language, OSes are pretty good these days about memory reclamation.

Share this post


Link to post
Share on other sites
Thanks for the reply guys, I am talking about C++. I was definitely getting a memory leak from it. When I set the string to anything other than "" I get memory leaks when the program exited. When I changed it to "", no memory leaks. Wish I could supply some sample code, but it's pretty imbedded. I changed the array in the class to be a point and allocated the array using 'new'. Then when I do a 'delete []' the strings seem to be freed and no memory leak.

Am I going crazy? You don't seem to think this is possible.

Share this post


Link to post
Share on other sites
In C++, whenever you want to dynamically allocate an array, using the new and delete[] operators is a great choice. In your situation, you can simply declare a variable of type char* and then allocate space for it using the new operator. Then, when you're done using it, call delete[] on it and it's cleaned up beautifully.

char* MyString;

MyString = new char[64]; //Memory allocated
delete[] MyString; //Cleaned up--no leaky

Share this post


Link to post
Share on other sites
Quote:
Original post by Teric
In C++, whenever you want to dynamically allocate an array, using the new and delete[] operators is a great choice.

No! Direct dynamic memory allocation and deallocation should always be your last choice. To represent text use std::string. For anything else use another container (i.e. std::vector, boost::multi_array, etc.). std::string will deallocate any memory it allocated when it is destroyed, even if that destruction is due to stack unwinding caused by an exception.

OP: How is your array declared and how are you detecting memory leaks? The most likely explanation is that your array is a static array that is constructed before your program enters main and destroyed after program execution leaves main and that your memory leak detector is running before your array is destroyed.

Enigma

Share this post


Link to post
Share on other sites
How are you checking for memory leaks?

most std::string implementations don't use new to allocate memory. They use some sorting of memory pooling technique, often with a static memory pool. This can sometimes be detected as a memory leak, but it isn't


It is safe to assume that containers in namespace std don't leak memory.

Share this post


Link to post
Share on other sites
What *exactly* were you using that appeared to leak?

How *exactly* did you change it such that it appears not to leak?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!