I have ;
class object {
// any private datas token in heap area
public :
~object () ;
};
<
The destructor function should contain all necessary stuffs to free up the memory object is occupying.
Since this object pointer is stored inside a vector, you should remove reference to it after destroying it. That is, erase that object* from the vector.
To delete the object on the end of that pointer, call delete myVector[i]
To then remove that pointer from in the vector, call myVector.erase(myVector.begin()+i)
You can use vector::erase..
And your object
destructor should delete
all the members you have allocated in heap.
You put in the destructor whatever your class needs to free its members.
As for the vector, it contains pointers, not object instances. As such, calling erase()
will only remove pointers from the vector, but will not free the objects that the pointers are pointing at. You have to free the objects separately, eg:
std::vector<object*> tmp;
tmp.push_back(new object);
...
std::vector<object*>::iterator iter = ...;
delete *iter;
tmp.erase(iter);
You can simply call delete tmp[i];
. But I would recommend to use std::vector< std::shared_ptr< object > >
instead.
You seem to be a bit confused. There are two concepts at play here:
tmp
, deallocate the objects referred to by the pointers within tmp
?object
class (~object
) do?These are not really that related. When you are finished with the tmp
vector, you must manually go through and call delete
on each of its elements in order to deallocate the memory occupied by the object
object that the element points to. This is assuming the elements were allocated with new
, of course.
The purpose of the object
destructor ~object
is to deallocate everything that the object
object owns, not to deallocate the object
object itself. If the object
object does not own any dynamically allocated data, it does not need to do anything.
In other words, when you write delete tmp[i]
, two things happen:
*(tmp[i])::~object()
is calledtmp[i]
is deallocatedNote that (2) happens even if (1) does absolutely nothing. The point of step (1) is to allow the object that is about to be deallocated to deallocate any of its member objects that need to be deallocated. The destructor's job is emphatically not to deallocate the object that it was invoked on.
By way of explicit example:
class object {
private:
int foo;
public:
object() : foo(42) {}
~object() { /* nothing to do here; foo is not dynamically allocated */ }
};
int main() {
vector<object*> tmp;
tmp.push_back(new object());
// Do some stuff with tmp
for (int i = 0; i < tmp.size(); ++i) {
delete tmp[i]; // Calls ~object and deallocates *tmp[i]
}
tmp.clear();
return 0;
}
Or, by contrast
class object {
private:
int* foo;
public:
object() : foo(new int()) { *foo = 42; }
~object() {
// Now since foo is dynamically allocated, the destructor
// needs to deallocate it
delete foo;
}
};
int main() {
vector<object*> tmp;
tmp.push_back(new object());
// Do some stuff with tmp
for (int i = 0; i < tmp.size(); ++i) {
delete tmp[i]; // Calls ~object (which deallocates tmp[i]->foo)
// and deallocates *tmp[i]
}
tmp.clear();
return 0;
}