Jump to content
xisto Community
Sign in to follow this  
kvarnerexpress

Free Memory After Out Of Scope?

Recommended Posts

Hi everyone,I'm working with stl maps, I seem to be experiencing a memory leak somewhere, the debugger isn't giving me any warning about it though. I thought maps and other containers are supposed to free themselves once they fall out of scope? Code:void function1(){ map<CString, set<CString> > local_map; // insert some stuff into local_map... local_map["key"].insert("somethin"); local_map["key"].insert("somethin else"); // for every key of local map, I would like to create a // 'minimap' that is just that one key and its associated // <set> values. for (map<CString, set<CString> >::iterator i = local_map.begin(); i != local_map.end(); i++) { // The memory for *pm will be deleted on the receiving end. map<CString, set<CString> > *pm = new map<CString, set<CString> >; pm->insert(pair<CString, set<CString> >(i->first, i->second)); PostMessage(WM_SENDMAP, (WPARAM)pm, NULL); } // local_map should deallocate itself right here yes?}void OnMessageFunctionReceiveMap(WPARAM w, LPARAM l){ // This is the minimap posted from function1. map<CString, set<CString> > *pReceivedMap = (map<CString, set<CString> > *)w; // Now I would like to set a member variable of // a thread object equal to the received mini map. m_pThreadObject->SetMap(pReceivedMap); // Now the memory allocated for this minimap in function1 // should be taken care of? delete pReceivedMap;}void CThreadObject::SetMap(map<CString, set<CString> > *map){ // I would like m_MemberLocalMap to be filled with the // values from *map, which it does, but does this // mean that m_MemberLocalMap will incorrectly still // be retaining any allocated memory from *map? m_MemberLocalMap = *map;}Again, I don't receive any memory leak warnings, but everytime I run the app I keep getting an extra 20k in memory usage. I have tested all this out before adding the <map>, with no problems so I'm pretty sure it has something to do with this.Thanks for any info!

Share this post


Link to post
Share on other sites

why don't you just do a new()...delete()or if you're in C, freelloc, etc. around thewhole thing?

Share this post


Link to post
Share on other sites

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  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.