Goleo8
Goleo8

Reputation: 135

Do I need to call the destructor of vector in this scenario

Now I implement a class and I need to use vector to hold some pointers.This is a global member. vector g_vIPControlCollection;

When the system finalize. I want to reclaim the memory. Then I define the destroy method.

void Destroy()
{
    int size = g_vIPControlCollection.size();
    if (size > 1)
    {
        for (int i = 0; i < size; i++)
        {
            g_vIPControlCollection[i]->Release();
        }
    }
    g_vIPControlCollection.clear();
    g_vIPControlCollection.~vector<IPersistorControl*>(); //Does this line is necessary?
}

My question is whether I need to call destructor of the vector? Thanks in advance. Your help will be greatly appreciated.

Upvotes: 0

Views: 370

Answers (4)

kiranpradeep
kiranpradeep

Reputation: 11211

  1. No. You shouldn't and needn't call destructor manually. That would result in destructor invocation two times ( 1 by you and 1 when object goes out of scope), affecting object state and your program might crash at best. See live demo

  2. It is good and recommended practice to clear memory allocated. But since it is a global object, and program is going to terminate, even if you don't free memory, it will reclaimed as process terminates.

  3. Try using std::unique_ptr<> for storing pointers in std::vector so that memory will be released when the clear method in invoked on std::vector. This way you don't have to iterate and manually invoke the Release method of each member of std::vector

Upvotes: 0

Scis
Scis

Reputation: 2984

No you should not, what you should do is use a unique_ptr to manage your IPersistorControl objects for example:

#include <iostream>
#include <vector>
#include <memory>
using namespace std;

struct Point{
    int x; int y;
    Point(int x,int y): x(x), y(y){}
    ~Point(){ cout<< "destroying " << y<<endl;}
};

int main() {
    {
        vector<unique_ptr<Point>> ps;
        ps.emplace_back(unique_ptr<Point>(new Point(1,2)));
        ps.emplace_back(unique_ptr<Point>(new Point(1,3)));
        ps.emplace_back(unique_ptr<Point>(new Point(1,4)));
    } // will call dtors here
    cout << "Example 1" <<endl;
    {
        vector<unique_ptr<Point>> ps;
        ps.emplace_back(unique_ptr<Point>(new Point(1,2)));
        ps.emplace_back(unique_ptr<Point>(new Point(1,3)));
        ps.emplace_back(unique_ptr<Point>(new Point(1,4)));
        ps.clear(); // will call them here
        cout << "Example 2" <<endl; 
    }

    return 0;
}

Note that if IPersistorControl is some object that needs a special type that requires some other method of "reclaiming" (e.g windows handles or file handles) you can specify a Deleter e.g:

unique_ptr<FILE, int(*)(FILE*)> filePtr(fopen("LALA.txt", "wb"), fclose);
                     ^                                            ^
                    Deleter type                              the actual Deleter   

Upvotes: 2

Mohit Jain
Mohit Jain

Reputation: 30489

No you should almost never call the destructor explicitly.

What you need to do here is just g_vIPControlCollection.clear(); that you are already doing. After this std::vector hardly keeps any memory (typically 12 bytes on 32 bit machines) and would be cleaned when the program ends.

Upvotes: 1

Bartek Banachewicz
Bartek Banachewicz

Reputation: 39390

No.

If you did that, after the call to Destroy it would be left in an invalid state. It will self destruct when its owner gets destroyed.

And your Destroy function should probably be a destructor, too.

Upvotes: 3

Related Questions