Sign in to follow this  

std::map memory leaks

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

Im working on a simple scripting language and am running into a problem with std::map. Ive been using the _Crt**** (IE _CrtDumpMemoryLeaks) stuff to check for memory leaks and found that std::map is producing a leak. After debugging ive found that _Myhead in the std::map class is what's causing the leak. This leak can be caused even if I dont add anything to the map. (std::map<std::string, IFunctionObject*> funcs is the declaration in my class). Any reason why this would happen? (If this isn't enough information, I can give more)

Share this post


Link to post
Share on other sites
It always depends - static and globals do not have a defined order of destruction. Therefore, leak detection might be executed before the memory is actually being freed. To be 100% sure you can create a class that has the map as a member. Dynamically allocate an instance of that class and delete it on shutdown. If you still experience memory leaks, it could be that the memory is not being freed (after all the implementation can still deal with static memory internally).
It should not be considered a problem anyway, since the OS will free the memory once the app is being unloaded.

Share this post


Link to post
Share on other sites
If the memory is released after your call to _CrtDumpMemoryLeaks, it will be reported as a 'leak' even though it really isn't one. This will typically happen for static/global objects (as darookie points out) - learn to interprete the output of that function. Non-repeated 'leaks' (as in, you only appear to leak one pointer, as opposed to one per element/loop/whatever) can generally be ignored.

Share this post


Link to post
Share on other sites
The class that contains it is global (makes things a lot easier) so that could be the problem. Is there a way I could force the std::map to clean itself up? Or should I consider something else?

Thanks,
Porthios

[EDIT]
That must've been the problem, I fixed it by adding `_CRTDBG_LEAK_CHECK_DF` to the _CrtSetDbgFlag call so it cleans everything up on exit and the leak disappeared. Thanks for the quick replies!

Share this post


Link to post
Share on other sites
Quote:
Original post by porthios
The class that contains it is global (makes things a lot easier) so that could be the problem. Is there a way I could force the std::map to clean itself up? Or should I consider something else?

Thanks,
Porthios

[EDIT]
That must've been the problem, I fixed it by adding `_CRTDBG_LEAK_CHECK_DF` to the _CrtSetDbgFlag call so it cleans everything up on exit and the leak disappeared. Thanks for the quick replies!
The thing is there wouldn't be a leak in std:map. What you are seeing is merely _CrtDumpMemoryLeaks giving you misleading information. The memory is freed (later) and there isn't actually any problem at all.
There is no spoon *ahem*, I mean leak.[smile]

Share this post


Link to post
Share on other sites
This is a common problem when using static stl containers in a DLL. One solution is to declare the static map as a pointer and use static init/destroy methods on the static class to allocate and deallocate the map itself on the heap. It's certainly not as pretty, and technically there are no leaks when you just declare it normally, but if you're writing library code that someone else will compile, it's not good form to have memory leaks (spoofed or not) popping up from your library.

You have to make the call purely on aesthetics.

Share this post


Link to post
Share on other sites

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