0

I have to call a function of a class from another function of the same class. But the problem is for some cases the current object can be destroyed. So for this case calling the function is creating errors. Is there any way to check whether the current object is destroyed or not from inside the class?

Another thread may first delete the current object and then create another object of the same class before calling function1 from inside function2. In that case, calling function1 would be valid, right?

For example,

#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>

using namespace std;

class example {

public:
    example(){}
    ~example() {}

    void function1() {
        // some code here
        return;
    }

    void function2() {
        //some code here
        // I want to check whether the current object is destroyed or not before calling function1() 
        function1();
    }
private:
    bool check;

};

int main()
{   
    example *exp=new example();
    exp->function2();
}
ksohan
  • 1,165
  • 2
  • 9
  • 23
  • 1
    How did the object get destroyed in the first place, while its members are still being called? Most likely, the program that encounters this case already triggered undefined behavior earlier. And no, there's no way to test whether a particular object pointer is valid, whether from the member function of that object or otherwise. – Igor Tandetnik May 31 '20 at 15:40
  • 1
    if `exp` has already been deleted then `exp->function2();` is Undefined Behaviour (as is calling any member function). So the test inside `function2` is already too late. – Richard Critten May 31 '20 at 15:40
  • Not using any standard functionality. Once an object is destroyed (via delete) it can be reused by a completely different class (or classes) and as such overwrite the memory. There is no runtime mechanism to know what a random memory block is being used as. – Martin York May 31 '20 at 15:41
  • Let's say, there is another thread that is using the same object and at some point, it destroyed the object. So there is no way to check before calling function1()? – ksohan May 31 '20 at 15:44
  • 1
    Does this answer your question? [Checking if this is null](https://stackoverflow.com/questions/1844005/checking-if-this-is-null) – Richard Critten May 31 '20 at 15:46
  • I have updated the description a little bit. @Richard Critten – ksohan May 31 '20 at 16:00
  • 1
    If you have multiple threads modifying and possibly destroying the same object, you need to use synchronization methods. Simply having multiple threads write to the same object without any protection is going to cause race conditions and Undefined Behaviour, regardless of whether the object has been destroyed or not. Please take a look at things like mutexes, condition variables, semaphores, and atomic variables. – alter_igel May 31 '20 at 16:04

1 Answers1

3

Is there any way to check whether the current object is destroyed or not from inside the class?

No, there is no way to do that.

I presume that "from inside the class" means "by using state of the object". Well, if the object is destroyed, then there is no state that could be observed. Any attempt would result in undefined behaviour.

Also, when the object has been destroyed, member functions of the object may not be called, so it is pointless to test that in function2 because if the object had been destroyed, then calling function2 would already result in undefined behaviour.

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • I have updated the description a little bit. Can you please check that? – ksohan May 31 '20 at 16:04
  • 1
    @KaziSohan If you call a member function of a destroyed object, then behaviour of the program is undefined. Whether you have created other objects is irrelevant in that regard. – eerorika May 31 '20 at 16:06