Is an object deleted when its member function is executed?

I have a pointer object for a singleton class.

Topic 1: performing a member function of the above object. Topic 2: Deletes the above object when the member function of the object is still being executed by thread 1.

What happens to Thread 1 where a member function is executed? Will execution stop halfway?

+4
source share
4 answers

This is probably undefined behavior if you are not particularly careful.

Removing an object while the thread is accessing it in an unordered (not synchronized, basically) way is undefined behavior, even if you're lucky.

Access to members of an object (not just starting an element: actual access to a member - calling a member function, reading a member variable, etc.) after synchronization with removal from the main thread is also an undefined behavior.

If you carefully synchronize with the main thread, then you do not get access to the element data or do not call another member function after this synchronization, and the main thread deletes the object after this synchronization, then you are fine.

Almost anything that leads to undefined behavior.

While undefined behavior can do almost anything, it would be unexpected for undefined behavior to force code in a member function to suddenly stop just because the object has been deleted. Most likely you will get segfaults, memory corruption, etc. Worst of all, in many cases, everything will work "just fine."

+4
source

You will most likely get undefined behavior. In thread 1, a seg error may occur or it may continue on its way if it does not access the element data (and does not make any virtual function calls) after the object is deleted. It also depends on what happens to the memory area after deletion. Is it cleaned up? Not written / not read? It all depends heavily on the implementation, and also depends on the data needs of your application in terms of reusing the data area to process other distributions.

Note - never delete an object until all its use has been completed. There are some exceptions when member functions delete the object that they use, provided that there is no longer access to the member after the delete point. But apart from the few cases that actually warrant removal in the penis, do not do this.

In the multi-threaded environment that you are describing, it is absolutely certain to coordinate the life of an object using the object, or it can be very difficult to debug (platform-dependent and not deterministic) behavior.

+2
source

Short answer: you should not do this ... You need to make sure that the object is not used before it is deleted.

In terms of what the C ++ standard indicates, this is (possibly) undefined behavior - something unexpected may happen. The basis for this is that undefined behavior is used to use any member (function or variable) of an object after the destruction of the object. Of course, if the member function looks like this:

void foo::func() { for(;;) { // do nothing here } } 

then this is NOT undefined behavior, and the stream can continue to work indefinitely, without any bad effect, and the clarity (I believe) is well defined (it just continues to work).

Of course, the execution of thread 1 will definitely not stop simply because the object is being deleted. That is, if the object does not actually contain the stream object for stream 1 - in this case, the destructor of the stream object can kill the stream.

In practice, if an object is deleted, its memory is freed, and the memory can be reused for some other object after some time. Depending on what the member function performs, for example, which parts of the object it refers to and how it uses these elements, it may cause a crash, or if it does something like x++; (where x is a member of a variable), it will change some memory that it no longer owns - and it can be quite difficult to debug, because it will look like something randomly changing other objects ... This is definitely NOT good ( and most likely it will happen only SOMETIMES, which makes it very difficult to determine when it will go wrong).

Nothing good will come of it.

You MUST make sure that the object is not used in any way to remove it. One way to do this, of course, is to have a thread object for thread 1 and kill the thread.

0
source

Undefined behavior is the correct answer. To avoid such things, consider using smart pointers, for example: http://www.boost.org/doc/libs/1_54_0/libs/smart_ptr/shared_ptr.htm or this: http://en.cppreference.com/w / cpp / memory / shared_ptr . They have a link counting mechanism that does not allow you to delete using an object.

0
source

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


All Articles