I have a Thread class like bellow
class Thread {
public:
Thread();
~Thread();
void start();
void stop();
}
So i need to call destructor from stop() method, is it a good way to do that?
I have a Thread class like bellow
class Thread {
public:
Thread();
~Thread();
void start();
void stop();
}
So i need to call destructor from stop() method, is it a good way to do that?
Technically yes, but be careful, you should not use the deleted object, this
and non-static members anymore:
delete this;
You can also call the destructor:
~Thread();
However, in a well designed code, you can avoid these types of self destructions.
Er'body saying don't call delete this
from within a member function but to notify the owner that the object is ready to be deleted.
I have a service that creates workers but doesn't care about them after they're created.
From within the service class:
Worker* w;
[...]
while (1) {
[...]
w = new Worker();
[...]
}
Then from within the worker class:
void Worker::doWork() {
[...]
[...]
delete this;
}
All the service class does is make new workers, so outside of the worker class there are no references to the worker except the pointer w
which is discarded every time a new worker is created.
In this case i think it makes sense to call delete this
inside the worker when it has no more work to do (as long as delete this;
is the last statement in doWork()
and there are no succesive member calls after doWork()
.
If you were storing every instance of created worker however, this wouldn't be true anymore.
You can use this syntax:
~Thread();
But I doubt if you really need this feature of C++. Maybe you should better design your class.
One legal case for explicitly calling destructors is in custom memory managers which you cannot use the delete
operator to delete the object.
No.
I think that it is bad practice to call destructor from inside the class code. If you need to do cleanup that is done also in the destructor you should use a cleanup() function to encapsulate that work, and have that function called from stop and from the destructor if relevant.
Obviously such a solution would require keeping the state of the object to know if it was already cleaned up or not, to avoid unneeded work and multiple freeing of resources which might no be yours anymore.
Specifically for your case, I'm not sure why you would want to delete the thread from the stop function, if there is some mechanism managing the threads - it should allocate/deallocate the threads from outside, and not the thread itself which deallocates its own memory when stopping. (The thread should perform cleanup as mentioned above, but no force a call to its own destructor)
No. Don't do this.
Have the thread owned by another object.
When you call stop()
the thread should tell its owner that it is ready to be deleted (make sure you lock yourself so the owner does not delete before you are finished). Then let the owner of the object do the clean up as appropriate (hopefully in the near future).
yes you can. that feature is used in COM as each object have a release function which decrement a private integer of reference count and if it reaches zero it delete the entire object