Vectors and Memory Leaks

Hi,

I’m using OF0073 in Xcode4.5.2 and am very new to C++. I want to create a little project with flocking particles, where their number and motion respond to the audio in volume. I’ve currently got (what I think) is a memory leak that crashes the app. When I run the Leak Instrument:

Leaked Object # Address Size Responsible Library Responsible Frame
Malloc < varying sizes > 424 < multiple > 314.88 KB cameraRibbonExampleDebug __gnu_cxx::new_allocator::allocate(unsigned long, void const*)

Here is the code I am using to add/remove particles from the ‘particles’ vector. I’m sure I’m doing something wrong…I know using new is probably the problem, but I’m lost as to how to do this correctly–is it OK to do this with a vector? I’ve tried looking at C++ forums, but I’m completely lost >:( Any guidance is appreciated!

  
void testApp::createParticles( )  
{  
  
    if ( particles.size() > 0 )  
    {  
        int diff = oldNum - numParticles ;  
  
        if ( oldNum > numParticles )  
        {  
  
            for ( int a = 0 ; a < diff ; a++ )  
            {  
                particles.pop_back() ;  
                
            }  
  
              
        }  
        else  
        {  
            
            int absDiff = abs( diff ) ;  
            for ( int b = 0 ; b < absDiff ; b++ )  
            {  
                Particle * particle = new Particle() ;  
                particle->setup( ofVec3f( 0 , 0, 0 ) ) ;  
                particle->followParticle = particles[0]->followParticle ;  
                particles.push_back( particle ) ;  
  
            }  
  
        }  
    }  
  
}  
  

there is no reason for you to use new there. (new operator)

vectors are actually dynamically allocating* whatever you put in them
(*dynamically allocating = saves it in “special” memory called heap)

when ever you use the new operator in c++ you must then release the object by calling delete. If you don’t do that then you create a “memory leak”

So what happens here (if this thing compiles at all) is that you are constantly creating new particles saving them on your special memory and then another pair of particles in the vector

when you pop_back a particle the vector deletes it but the copy that was allocated in the heap stays there as junk memory.

A memory that your program lost track to.

once your heap is filled your program crash

Also you might want to look into iterators

You’re storing pointers to Particles in your vector. pop_back() will destroy the thing at the end of your vector, but that “thing” is just a Particle pointer, not the Particle pointed to. A pointer takes up memory, just like any other data type, and it’s that memory that is being stored in the vector and therefore freed.

You first need to delete the Particle pointed to, so something like:

  
  
Particle* back = particles.back();  
delete back;  
particles.pop_back();  
  

or more concisely:

  
  
delete particles.back();  
particles.pop_back();  
  

I didn’t test that, but hopefully you get the idea.

a pointer is a pointer,when you use the new operator you are just using the pointer to access that memory that you have dynamically allocated by using new. vectors automatically make a copy of the chunk of memory that you pushed inside them. so you are creating two particles while you are in-fact only using one
so you still create a memory leak with that code pcoccoll you are just deleting a chunk of memory inside the vector thus leaving the vector empty. but when you pop_back you delete an empty array. the address of the memory that you dynamically allocated when you pushed back the particle is lost

you should either not use pointers or delete the pointer right after you have push it inside the vector.

I’m not sure exactly what you’re trying to say, kkkkkkkk, but I don’t think you understand pointers. The OP is filling a std::vector<Particle*>. The push_back() method extends the array and places of copy of its input on the back. In this case, the input is a pointer to a Particle object. If you “delete the pointer” after putting it on the back of the array, you’re actually deleting the object pointed to, so you now have a pointer in your vector that points to freed memory. This results in undefined behavior.

yeah I think you’re right my misconception was on how vectors really work, I thought that they create a new copy of the memory, but now that I think about it it may just create a copy of a pointer if you have an array of pointers to particles so you don’t create a new particle when you put it in the pointer you just create a new pointer.

in that case you are 100% right.

I thought that vectors created a copy of the memory not the object…

just one question though… why put pointers inside a vector? what’s the benefit to that? why not just put an object?

kkkkkkkk, I’ve definitely thought about and tried what you suggested and got the undefined behaviour problems pcoccoli described.

I’ve tried putting objects in the vector, but cannot access them correctly. As I’m a n00b, I’m probably doing it wrong :S…

pcoccoli, I’ll try your suggestion…thanks–this is so helpful!

pcoccoli, thank you so much! That has fixed the memory leak!