Is there any way to check whether the "Delete()" has been invoked already?

Hello,Is there any way to check whether the “Delete()” has been invoked already?

Today? Not in a safe way, no.

Once vtkWeakPtr is merged, that will be a reliable way to detect it (vtkWeakPointer is a rat’s nest of TOCTOU and race conditions; never use it).


The best approach to that is by using smart pointers, which keep track the number of references being used and deallocate the object automatically. If you still need to use crude pointers, your code should set them to nullptr right after a Delete[] and use an assert or test them with a if( fooptr == nullptr ) ... before using. Still, if you don’t want to add lots of = nullptr;s and if()s to your code:

  1. In Windows, you can use the IsBadReadPtr() API function: IsBadReadPtr function (winbase.h) - Win32 apps | Microsoft Learn.
  2. On POSIX OSes, your code can install a handler to SIGSEGV signals to capture the crash (yes, it’s handleable unlike other crashes). Take a look at this: . Bear in mind that ill-written signal handlers can result in an OS-level infinite loop which likely is only solvable by rebooting.

I hope this helps,


Neither of these help if something got allocated in that place between deletion and checking. Just don’t touch pointers you’ve told the system that you’re done with (via free, delete, or vtkObject::Delete).

Yes, there is a small chance of that happening. That’s why the mentioned methods come last. Still, if the memory intervals are taken by other processes, they still can be used to test for pointer validity.