简体   繁体   中英

Is map container declared in class getting destroyed when the class is destroyed?

When the object a is destroyed, is the personsInHouse map also destroyed or I need to destroy it in destructor? Will it create memory leak if I don't?

class A {
public:
    map<unsigned int, unsigned int> personsInHouse;
};

int main(){
    A a;
   A.hash[10] = 23;
};

The lifetime of personsInHouse is automatic because you are storing it by value , and its lifetime is the lifetime of the parent object. Because you create a by value , its destructor is called when it goes out of scope, and the destructor of an object automatically calls the destructors of the objects it contains. So you do not need to destroy personsInHouse , like you do not need to destroy a .

If personsInHouse was a pointer and you created a map<unsigned int, unsigned int> in dynamic storage with new and stored a pointer to it in personsInHouse , then you would need to manually deallocate the memory that personsInHouse was pointing to in the destructor of A via delete . But that's not the case in the code you posted.

What you have done is the good way to do it: prefer to store every object that you can by value so that you don't have to worry about lifetime management of dynamic objects.

Yes, it is. When the destructor of a class is run, the destructors of all its members are run. To be precise, the order is:

  1. Body of the destructor is run
  2. All members, in reverse order of construction, are destructed
  3. All non-virtual base classes, in reverse order of construction, are destructed
  4. All virtual base classes, in reverse order of construction, are destructed

In general, if you don't have pointers, you can expect to also not have memory leaks. This is not always the case: you may be using a leaky function, or some function may be performing dynamic allocation and then returning a reference to the object. The situation can be further improved by using smart pointers.

A useful technique for avoiding memory leaks in C++ is RAII : all standard containers follow it, which is why there's no need to explicitly clear() a container before it goes out of scope. The basic principle is to make classes clean up all their resources in their destructors, and then make dedicated classes for that so that most of your classes need not worry about it.

Do note that "class members" are strictly non-static members defined at class scope. If you have

struct S {
    int* p;
};

then p is the only member of S , and when S goes out of scope, p will be destroyed (which doesn't generally involve anything happening, except perhaps an adjustment of the stack pointer). If you at some point do S s; sp = new int; S s; sp = new int; then p will still be the only member, and the object pointed to by p will not be one, and will therefore not be destroyed when s goes out of scope. For that to happen, you will need to manually do delete sp; , which corresponds to the general rule of every new needing to have a corresponding delete (idem for new[] and delete[] ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM