Andreas
Andreas

Reputation: 7550

Do I need to clean up non-pointer data members?

If I don't use new to allocate the data members of a class, is there still any clean-up that needs to be done in the destructor? For example, in the following case, will I need to delete the vector? Or does it depend on MyType?

class A {
  A();
  ~A();
  MyType x;
  std::vector<MyType> v;
};
A::A() {
  x = MyType(42);
  v = std::vector<MyType>(5);
}
A::~A() {
  // what goes here?
}

Upvotes: 3

Views: 1895

Answers (6)

sandhya
sandhya

Reputation: 11

The vector is automatically deleted at time of destruction. However, if you store pointers in a vector, E.g. If you say vector<Foo *> v;, you'll need write code to delete the pointers in the vector. One way to avoid this is to use vector<std::unique_ptr<Foo>> or vector<std::shared_ptr<Foo>>

Upvotes: 1

BrunoB
BrunoB

Reputation: 357

For class A no, you don't, since it doesn't have any pointer member allocated using new or malloc.

Beware that if your class/struct MyType have any pointers as a member, the destructor of MyType is the responsible to free the memory of such pointer, but you don't have to worry with that in class A, since when its destructor is called, the destructor of the vector calls the destructor of MyType.

Upvotes: 0

Luchian Grigore
Luchian Grigore

Reputation: 258618

To be pedantic, it depends on what MyType is. If you have typedef char* MyType and you allocate memory for MyType, and you want that memory to be owned by the object, then yes, you need a destructor.

Otherwise, you don't need to free memory for anything that wasn't allocated with new or malloc, so no.

Your class shouldn't even have a destructor at all. There's a general consensus that you should only have a destructor when you actually need it. Having a destructor also implies implementing an assignment operator and copy constructor (the rule of three). If you're not managing any memory, it's better to rely on the ones provided by the compiler - i.e. the compiler will generate these three if you don't.

Also, your constructor should look like this:

A::A() : x(42), v(5){
}

Otherwise your members will be initialized and then assigned to, which is wasteful.

Upvotes: 9

Spo1ler
Spo1ler

Reputation: 4369

No, you don't have to. Their constructors will be called implicitly.

Upvotes: 0

Seva Alekseyev
Seva Alekseyev

Reputation: 61378

Your class does not need an explicit destructor. The vector's destructor will be invoked automatically anyway. And since the vector object itself (as opposed to the data inside) is not dynamic, you don't have to delete it. In fact, that'd be a syntax error, as v is not a pointer.

Upvotes: 4

user1203803
user1203803

Reputation:

The destructors are automatically called, so you don't need to do that. Also, for pointers use smart pointers (such as std::unique_ptr) instead of deleteing them manually.

class A {
  A();
  ~A();
  MyType x;
  std::vector<MyType> v;
};
A::A() : x(42), v(5) { }
A::~A() {
  // x.~MyType() implicitly called.
  // v.~std::vector<MyType>() implicitly called.
}

Upvotes: 1

Related Questions