There's a simple rule to avoiding memory leaks:
For every new, delete.
For every new, delete.
For every malloc(), free().
Things aren't always that simple, of course, but here they are----you've allocated a B on the heap, stored its address only in a local pointer (so far as you've shown us), and not cleaned it up anywhere. Textbook memory leak.
I don't understand why when b is created on the stack. It would cause memory leaks.
I commented that part out, the leaks were gone. Any thoughts?
b is a "pointer" it is created on the stack, and is destroyed, like any other stack variable.
b does not "own" its allocated memory.
It is not b that is leaking, but the "new B".
Don't confuse pointer and pointee
Is your question related to IO?
Read this C++ FAQ LITE article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
The answer is to not use bare pointers unless you have to or they are just observers. Any time ownership is concerned you should use a smart pointer such as boosts shared_ptr or std::shared_ptr if your compiler supports it. Then when the smart pointer goes out of scope the memory will be automatically reclaimed by the smart pointers destructor ( or at least its reference count will be decremented and when that hits zero the object will be deleted ). Read up on the principle of 'Resource Acquisition Is Initialisation (RAII)' and smart pointers.
Get Microsoft Visual C++ Express here or CodeBlocks here.
Get STLFilt here to radically improve error messages when using the STL.
Get these two can't live without C++ libraries, BOOST here and Loki here.
Check your code with the Comeau Compiler and FlexeLint for standards compliance and some subtle errors.
Always use [code] code tags [/code] to make code legible and preserve indentation.
Do not ask for help writing destructive software such as viruses, gamehacks, keyloggers and the suchlike.
Others have already said it but this is another take on it,
If you instead of this
B *b = new B;
think of it like this (which is logically equivalent),
b = new B;
the situation becomes clearer. The first line declares a pointer variable b of type B to be allocated on the stack. The second line allocates a B type object on the heap and assigns its pointer to b. It's only the allocation of the B object on the heap that can cause a leak. To prevent it you must put in this line when the object isn't used anymore.
Some languages with built-in automatic garbage collection, such as Java and C#, will handle the deletion automatically but not C++. In C++ every new must have a matching delete (and every new a delete) or you have a memory leak on your hands.
When ever it becomes hard to determine when and where an object is to be deleted it's time to consider putting a so called reference counting "smart" pointer in charge of the situation. It will delete the object when it's no longer pointed to from anywhere anymore and thus can be safely deleted.
Last edited by nuzzle; March 22nd, 2011 at 01:37 AM.