How can I determine if a C++ object has been deallocated?

前端 未结 6 1801
情话喂你
情话喂你 2020-11-29 13:38

I have a previous question that was answered in which I describe difficulties catching an exception when I try to access an object that has been deallocated by a third-party

6条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-29 14:16

    create a destructor that notifies upon desrtruction. It could even toggle a bool, if you wanted to check it during runtime.

    class free
    {
    
    public:
    
        ~free() { cout << "Im being freed " << endl; } //destructor output
    };
    

    This will output in console that its being freed. After being freed, any reference to it will be undefined behaviour, even if it appears to exist.

提交回复
热议问题