I have the following code and was wondering if it is a memory leak

#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <vector>

using namespace std;

template <class T>
class live {
  vector<T> _data;
  live<T> *_next, *_temp;
public:
  live(int elements, int chunk = 10) : _next(NULL)
  {
    if (elements > chunk)
    {
      _data.reserve(chunk);
      _next = new live<T>(elements - chunk, chunk);
    }
    else _data.reserve(chunk);
  }
  
  ~live() { if (_next) { _next->~live(); delete _next; } }

};

Do I have to call the destuctor of the vector because I allocated:
_next = new live<T>(elements - chunk, chunk);
or is it called automatically when it becomes out of scope?

There is no need to call _next->~live(); since it is called when using delete _next; and this will likely cause some problems, as the memory will be freed twice. You do not need to free memory for the vector as it is contained within the class.

So basically this is all that is required?

~live() { if (_next) delete _next; }

for the destuctor?

Yup, that should do it.

So basically this is all that is required?

~live() { if (_next) delete _next; }

for the destuctor?

you don't need the 'if' either, if _next is NULL then delete will not create any problems.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.