Can I name a destructor from a class method?

I have a Thread class, like bellow

class Thread { public: Thread(); ~Thread(); void start(); void stop(); } 

So, I need to call the destructor from the stop () method, is this a good way to do this?

+6
source share
5 answers

Yes

 delete this; 

But be careful. You no longer have to use a remote object, this and the non-static elements.

Another way is to call the destructor

 ~Thread(); 

But the question is, why do you need to call the destructor ?! This is not logical. You can write resource management code in a private method and call it.

+5
source

You can use this syntax:

 ~Thread(); 

But I doubt that you really need this C ++ function. You might be better off designing your class.

One legal case for explicitly calling destructors is in user memory managers, which you cannot use with the delete operator to delete an object.

+2
source

No.

I think the wrong practice is to call the destructor from inside the class code. If you need to perform a cleanup that also runs in the destructor, you must use the cleanup () function to encapsulate this work and have this function called from the stop and from the destructor, if necessary.

Obviously, such a solution would require that the state of the object be known whether it has already been cleared or not, in order to avoid unnecessary work and multiple release of resources that can no longer be yours.

In particular, for your case, I'm not sure why you want to remove a stream from the stop function, if there is some kind of flow control mechanism - it should allocate / deactivate flows from the outside, and not the stream itself that frees its memory when it stops. (The thread should perform the cleaning as described above, but does not require calling its destructor)

0
source

No. Do not do that.

Has a thread belonging to another object.

When you call stop() , the thread must tell its owner that it is ready to be deleted (make sure you lock yourself so that the owner does not delete before you finish). Then let the owner of the facility clean up as required (hopefully in the near future).

0
source

Er'body says that do not call delete this from the member function, but to notify the owner that the object is ready to be deleted.

I have a service that creates workers, but does not care about them after they are created.

From the class of service:

 Worker* w; [...] while (1) { [...] w = new Worker(); [...] } 

Then from within the working class:

 void Worker::doWork() { [...] [...] delete this; } 

The entire service class makes new workers, so there are no references to the worker outside the working class, except for 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 he has nothing more to do (while delete this; is the last statement in doWork() and there are no successful member calls after doWork() .

If you saved each instance of the created worker, this will not be so.

0
source

Source: https://habr.com/ru/post/958906/


All Articles