How does ptr_vector manage memory?

I am currently in C ++ for a lower level of coding with opengl. I come from the heavy background of objc, so I have some understanding of memory management, but I can't figure out how the boost library handles container types like ptr_vector.

I think my problem is that I don’t know how it ptr_vectorcontrols the destruction of itself and its objects.

Please take a look at the following code:

// Header file
...
ptr_vector<IObject3D> objects;
...

// Implementation file
...
void ApplicationEngine::init()
{
    WavefrontObject3D *object = new WavefrontObject3D("Ninja.obj");
    objects.push_back(object); 
}
...

So, for the actual question : am I creating a leak here through the "object" variable?

objc: alloc init WavefrontObject3D object, , release , .

a delete object push_back, WavefrontObject3D object. , ptr_vector object. ?

, : , ApplicationEngine, - ptr_vector ,

+5
4

, . ptr_* , , .

, undefined behavior, .

: , ptr_vector , .

ptr_vector. , .

template <typename T>
class ptr_vector {
public:
  // assume control over it
  void push_back(T* x) 
  { if(x) c_.push_back(x); else throw bad_pointer(); }

  ~ptr_vector() { 
    // delete everything that is stored here
    for(auto x : c_)  delete x;
  }
private:
  std::vector<T*> c_;
};


// a user class
struct user_class {
  void addSomething() { x.push_back(new int(23)); }
  ptr_vector<int> x;
};

, ptr_vector , . .

+5
void push_back( T* x );

: x!= 0 : : bad_pointer if x == 0 :

template
    < 
        class T, 
        class CloneAllocator = heap_clone_allocator,
        class Allocator      = std::allocator<void*>
    >
    class ptr_vector : public ptr_sequence_adapter
                              <
                                  T,
                                  std::vector<void*,Allocator>,
                                  CloneAllocator
                              >

, CloneAllocator , ptr_vector, heap_clone_allocator ( CloneAllocator) .

http://www.boost.org/doc/libs/1_50_0/libs/ptr_container/doc/reference.html#class-heap-clone-allocator

+2

ptr_vector , , , , .. , ptr_vector . delete ( , , ptr_vector). , ptr_vector .

ApplicationEngine, ptr_vector, , , , .

0

ptr_containers , , . .

0
source

All Articles