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

**URL:** https://discourse.vtk.org/t/is-there-any-way-to-check-whether-the-delete-has-been-invoked-already/10324
**Category:** Support
**Created:** [December 28, 2022, 7:35am UTC](https://discourse.vtk.org/t/is-there-any-way-to-check-whether-the-delete-has-been-invoked-already/10324 "2022-12-28T07:35:23Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![AlexLuya](https://discourse.vtk.org/user_avatar/discourse.vtk.org/alexluya/32/4036_2.png) [@AlexLuya](https://discourse.vtk.org/u/AlexLuya)
#### Post date: [December 28, 2022, 7:35am UTC](https://discourse.vtk.org/t/is-there-any-way-to-check-whether-the-delete-has-been-invoked-already/10324/1 "2022-12-28T07:35:23Z")

</div>

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

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.vtk.org/u/ben.boeckel)
#### Post date: [December 29, 2022, 8:47pm UTC](https://discourse.vtk.org/t/is-there-any-way-to-check-whether-the-delete-has-been-invoked-already/10324/2 "2022-12-29T20:47:44Z")

</div>

Today? Not in a safe way, no.

Once [`vtkWeakPtr`](https://gitlab.kitware.com/vtk/vtk/-/merge_requests/8686) 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).

---

<div class="post-metadata">

### Author: ![Paulo\_Carvalho](https://discourse.vtk.org/user_avatar/discourse.vtk.org/paulo_carvalho/32/370_2.png) [@Paulo\_Carvalho](https://discourse.vtk.org/u/Paulo_Carvalho)
#### Post date: [January 2, 2023, 6:28pm UTC](https://discourse.vtk.org/t/is-there-any-way-to-check-whether-the-delete-has-been-invoked-already/10324/3 "2023-01-02T18:28:48Z")

</div>

Hello,

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](http://msdn.microsoft.com/en-us/library/aa366713(VS.85).aspx).
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: [https://www.linuxquestions.org/questions/programming-9/sigsegv-handler-segmentation-fauld-handler-277790/](https://www.linuxquestions.org/questions/programming-9/sigsegv-handler-segmentation-fauld-handler-277790/) . 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,

Paulo

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.vtk.org/u/ben.boeckel)
#### Post date: [January 3, 2023, 9:58pm UTC](https://discourse.vtk.org/t/is-there-any-way-to-check-whether-the-delete-has-been-invoked-already/10324/4 "2023-01-03T21:58:32Z")

</div>

> [@Paulo\_Carvalho](#):
>
> In Windows, you can use the `IsBadReadPtr()` API function

> [@Paulo\_Carvalho](#):
>
> On POSIX OSes, your code can install a handler to SIGSEGV signals to capture the crash (yes, it’s handleable unlike other crashes).

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`).

---

<div class="post-metadata">

### Author: ![Paulo\_Carvalho](https://discourse.vtk.org/user_avatar/discourse.vtk.org/paulo_carvalho/32/370_2.png) [@Paulo\_Carvalho](https://discourse.vtk.org/u/Paulo_Carvalho)
#### Post date: [January 3, 2023, 10:23pm UTC](https://discourse.vtk.org/t/is-there-any-way-to-check-whether-the-delete-has-been-invoked-already/10324/5 "2023-01-03T22:23:30Z")

</div>

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.
