0

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.

Chnossos
  • 9,971
  • 4
  • 28
  • 40
Goleo8
  • 125
  • 9

4 Answers4

3

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.

Bartek Banachewicz
  • 38,596
  • 7
  • 91
  • 135
2

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   
Scis
  • 2,934
  • 3
  • 23
  • 37
1

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.

Community
  • 1
  • 1
Mohit Jain
  • 30,259
  • 8
  • 73
  • 100
0
  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
kiranpradeep
  • 10,859
  • 4
  • 50
  • 82
  • As the Release is not the destructor of the object and it is only used to decrease the reference counter. Does the smart pointer can call it automatically when the vector clears all the members? – Goleo8 Jan 15 '15 at 11:54
  • @Goleo8 No. It won't. But if you wanted a reference counted object, wasn't `std::shared_ptr<>` the better choice ? I mean who ever ( client ) gets the objects, should received it as `std::shared_ptr<>`. For more http://stackoverflow.com/a/11824317/1180117 – kiranpradeep Jan 15 '15 at 11:57