-
February 21st, 2014, 09:05 PM
#1
Popping from STL Containers
I've been working on a project that involves storing pointers to dynamically allocated class objects in an STL list, but trying to run it something's going wrong.
list<Actor*>::iterator it;
for(it = m_actors.begin() ; it != m_actors.end() ; ++it)
{
delete *it;
}
m_actors.clear();
//where Actor is a user defined class
Every element of the list was allocated using new, but I nevertheless get this runtime error from XCode:
BugBlast(4041,0x7fff7f1e3180) malloc: *** error for object 0x100601610: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
It runs okay if I do something like this instead:
Actor* killer;
while(!m_actors.empty())
{
killer = m_actors.front();
m_actors.pop_front();
}
But it seems like that has a memory leak. Does pop_front() just call the destructor for the object, or will it delete a dynamically allocated chunk of memory? If not, how can I do that deletion to avoid a memory leak?
Thanks for the help!
-
February 22nd, 2014, 01:01 AM
#2
Re: Popping from STL Containers
Originally Posted by ThinkTwice
Every element of the list was allocated using new, but I nevertheless get this runtime error from XCode:
That code is not wrong. What *is* wrong is what you're doing with these pointers. If you corrupted memory in any way between the time you created those objects and the time you destroy those objects, then all bets are off as to how your code will run.
It runs okay if I do something like this instead:
Don't waste time with that code. The loop isn't the problem -- the problem is somewhere else in your program that is corrupting the memory.
Regards,
Paul McKenzie
-
February 22nd, 2014, 05:34 AM
#3
Re: Popping from STL Containers
When posting code, please use code tags. Go advanced, select the code and click '#'
Code:
list<Actor*>::iterator it;
for(it = m_actors.begin() ; it != m_actors.end() ; ++it)
{
delete *it;
}
m_actors.clear();
As Paul states in his post #2, this code is not wrong. The problem is that the memory you are trying to delete is invalid.
Code:
Actor* killer;
while(!m_actors.empty())
{
killer = m_actors.front();
m_actors.pop_front();
}
This will run without error as you are not trying to delete the memory as you don't do anything with killer - but you will have a memory leak as you are not freeing any memory. When you do try to delete the 'killer' memory you'll get the same problem as with the other code.
The problem, as Paul states, is elsewhere in your code. Is the problem re the 'pointer being freed was not allocated' occurring for every element or just one or just some? How are you allocating the memory using new? What debugging of the program have you done? As a quick test, after you have allocated all the pointers to the list, then try straight away to delete them. You'll then know whether the allocation/de-allocation works OK or not. Assuming it does, then you'll need to debug the rest of your code to see where the pointers are being corrupted. It's possible you have a buffer overflow issue somewhere which is walking over your list memory so corrupting it.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
February 22nd, 2014, 02:07 PM
#4
Re: Popping from STL Containers
Originally Posted by ThinkTwice
If not, how can I do that deletion to avoid a memory leak?
One reason for your current problem may be that you have accidentally added the same Actor object pointer twice into the list. Then you will be deleting the same object twice which leads to unidentified program behaviour.
To solve this in a more fundamental way I suggest you have a look at memory management by way of std::shared_ptr (a so called reference counting smart pointer which is part of C++ as of version 11).
-
February 23rd, 2014, 02:52 AM
#5
Re: Popping from STL Containers
Thank you all for the help! I found the issue where I was accidentally still pushing another copy of the previous item onto the list in one case of a break statement.
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
|