|
-
October 6th, 2006, 03:22 PM
#1
[RESOLVED] Managing pointers with STL map
I am having trouble with map containers. I have an information class called CInfo, and two map containers. I have typedef MAP_ONE as a <int, CInfo*>, and typedef MAP_TWO as a <int, MAP_ONE*>
I am able to use the containers well enough, but I am having trouble cleaning it up properly. Because the container class does not free the memory of the class the pointer is pointing to, I wanted to iterate through the maps and free up the memory myself. My first attempt was the code below. It resulted in a memory error when it tried to execute the code p_map_One->clear(); I then thought maybe once I have free the memory that the pointer was using, I should set the pointer in the map to NULL, so I added in the extra lines that are commented out below. When I uncomment these 2 lines I receive no errors, but when I observe the memory usage it looks like there is still a leak. Can anyone tell me if the below code is incorrect? Did I free up the memory correctly?
Code:
//At this point I have a member variable MAP_TWO m_map_Two, which
//has 2 entries. And for each entry there are 2 entries of MAP_ONE*
MAP_ONE::iterator itrOne = NULL;
MAP_ONE* p_map_One = NULL;
CInfo* pInfo = NULL;
for (MAP_TWO::iterator itr = m_map_Two.begin(); itr != m_map_Two.end(); ++itr)
{
p_map_One = itr->second;
for (itrOne = p_map_One->begin(); itrOne != p_map_One->end(); ++itrOne)
{
pInfo = itrOne->second;
delete pInfo;
//itrOne->second = NULL;
}
p_map_One->clear();
delete p_map_One;
//itr->second = NULL;
}
m_map_Two.clear();
Last edited by nice_guy_mel; October 6th, 2006 at 03:38 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|