# Trying to delete object with non-zero reference count.

**URL:** https://discourse.itk.org/t/trying-to-delete-object-with-non-zero-reference-count/5807
**Category:** Engineering
**Tags:** registration
**Created:** [March 16, 2023, 9:59am UTC](https://discourse.itk.org/t/trying-to-delete-object-with-non-zero-reference-count/5807 "2023-03-16T09:59:23Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![ma1282029525](https://discourse.itk.org/user_avatar/discourse.itk.org/ma1282029525/32/3084_2.png) [@ma1282029525](https://discourse.itk.org/u/ma1282029525)
#### Post date: [March 16, 2023, 9:59am UTC](https://discourse.itk.org/t/trying-to-delete-object-with-non-zero-reference-count/5807/1 "2023-03-16T09:59:23Z")

</div>

When my program exits, the title appears. Does it represent my memory leak？

---

<div class="post-metadata">

### Author: ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)
#### Post date: [March 16, 2023, 2:35pm UTC](https://discourse.itk.org/t/trying-to-delete-object-with-non-zero-reference-count/5807/2 "2023-03-16T14:35:37Z")

</div>

Probably. It is hard to tell without more information.

---

<div class="post-metadata">

### Author: ![ma1282029525](https://discourse.itk.org/user_avatar/discourse.itk.org/ma1282029525/32/3084_2.png) [@ma1282029525](https://discourse.itk.org/u/ma1282029525)
#### Post date: [March 17, 2023, 1:21am UTC](https://discourse.itk.org/t/trying-to-delete-object-with-non-zero-reference-count/5807/3 "2023-03-17T01:21:40Z")

</div>

do you have any method about checkthe itk memory lack ?

---

<div class="post-metadata">

### Author: ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)
#### Post date: [March 17, 2023, 12:56pm UTC](https://discourse.itk.org/t/trying-to-delete-object-with-non-zero-reference-count/5807/4 "2023-03-17T12:56:06Z")

</div>

I remember that VTK can bring up a dialog with a message similar to “Trying to delete object with non-zero reference count”. But I don’t think ITK does that. @blowekamp @mihail.isakov Do you agree?

---

<div class="post-metadata">

### Author: ![mihail.isakov](https://discourse.itk.org/letter_avatar_proxy/v4/letter/m/c4cdca/32.png) [@mihail.isakov](https://discourse.itk.org/u/mihail.isakov)
#### Post date: [March 17, 2023, 2:40pm UTC](https://discourse.itk.org/t/trying-to-delete-object-with-non-zero-reference-count/5807/5 "2023-03-17T14:40:02Z")

</div>

ITK has the warning [here](https://github.com/InsightSoftwareConsortium/ITK/blob/0afd0ddf4bbe1ded5ce6af06af2cd3babffcb086/Modules/Core/Common/src/itkLightObject.cxx#L182)

```auto
LightObject::~LightObject()
{
  /**
   * warn user if reference counting is on and the object is being referenced
   * by another object.
   */
  if (m_ReferenceCount > 0)
  {
    // A general exception safety rule is that destructors should
    // never throw. Something is wrong with a program that reaches
    // this point anyway. Also this is the least-derived class so the
    // whole object has been destroyed by this point anyway. Just
    // issue a warning, do not call `itkExceptionMacro`.
    itkWarningMacro("Trying to delete object with non-zero reference count.");
  }
}

```

I have the warning at one point, seems to be harmless, _Valgrind_ doesn’t detect leak at this point, so far nothing happens. Probably it is my mistake somewhere, not sure. I shall look again little later.

Edit:  
Also see this [post](https://github.com/InsightSoftwareConsortium/ITK/pull/2744#issuecomment-922542576). There is a minimal example.

---

<div class="post-metadata">

### Author: ![mihail.isakov](https://discourse.itk.org/letter_avatar_proxy/v4/letter/m/c4cdca/32.png) [@mihail.isakov](https://discourse.itk.org/u/mihail.isakov)
#### Post date: [March 17, 2023, 10:07pm UTC](https://discourse.itk.org/t/trying-to-delete-object-with-non-zero-reference-count/5807/6 "2023-03-17T22:07:35Z")

</div>

> Probably it is my mistake somewhere, not sure. I shall look again little later.

I have found my mistake. I used _itk::MapContainer_ on a stack.

```cpp
#include <itkMapContainer.h>
int main(int, char**)
{
	itk::MapContainer<int, int> map;
	return 0;
}

```

```auto
r@deb2:~/tmp/test5/build$ ./test 
WARNING: In /home/r/itk/ITK/Modules/Core/Common/src/itkLightObject.cxx, line 182
LightObject (0x7ffc902d3020): Trying to delete object with non-zero reference count.

```

It should be:

```cpp
#include <itkMapContainer.h>
int main(int, char**)
{
	itk::MapContainer<int, int>::Pointer map = itk::MapContainer<int, int>::New();
	return 0;
}

```

```auto
r@deb2:~/tmp/test5/build$ ./test 
r@deb2:~/tmp/test5/build$

```

---

<div class="post-metadata">

### Author: ![ma1282029525](https://discourse.itk.org/user_avatar/discourse.itk.org/ma1282029525/32/3084_2.png) [@ma1282029525](https://discourse.itk.org/u/ma1282029525)
#### Post date: [March 22, 2023, 1:40am UTC](https://discourse.itk.org/t/trying-to-delete-object-with-non-zero-reference-count/5807/7 "2023-03-22T01:40:08Z")

</div>

thank you,I resolved an error that is the same as yours, but I still have a warning, and when I debug a breakpoint, the system already prints that warning the first time I enter the destructor.

---

<div class="post-metadata">

### Author: ![blowekamp](https://discourse.itk.org/user_avatar/discourse.itk.org/blowekamp/32/79_2.png) [@blowekamp](https://discourse.itk.org/u/blowekamp)
#### Post date: [March 22, 2023, 11:47am UTC](https://discourse.itk.org/t/trying-to-delete-object-with-non-zero-reference-count/5807/8 "2023-03-22T11:47:59Z")

</div>

> [@mihail.isakov](#):
>
> I have found my mistake. I used _itk::MapContainer_ on a stack.

@Niels_Dekker Is this a bug? I thought we had a mechanism to prevent these object from being constructed on the stack?

---

<div class="post-metadata">

### Author: ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)
#### Post date: [March 22, 2023, 5:16pm UTC](https://discourse.itk.org/t/trying-to-delete-object-with-non-zero-reference-count/5807/9 "2023-03-22T17:16:56Z")

</div>

There is a [mechanism via a macro](https://github.com/InsightSoftwareConsortium/ITK/blob/v5.3.0/Modules/Core/Common/include/itkMacro.h#L399-L411) which is usually applied to filters. Maybe it was forgotten for `MapContainer`?

---

<div class="post-metadata">

### Author: ![Niels\_Dekker](https://discourse.itk.org/letter_avatar_proxy/v4/letter/n/9d8465/32.png) [@Niels\_Dekker](https://discourse.itk.org/u/Niels_Dekker)
#### Post date: [March 22, 2023, 6:06pm UTC](https://discourse.itk.org/t/trying-to-delete-object-with-non-zero-reference-count/5807/10 "2023-03-22T18:06:23Z")

</div>

Apparently the `MapContainer()` default-constructor was originally declared `protected`:

> <https://github.com/InsightSoftwareConsortium/ITK/blob/f0e0c8acf5a0506d34438417e669f3045ecdcc95/Code/Common/itkMapContainer.h#L106-L116>

It was then afterwards declared `public` instead, on November 30, 2001, commit [ENH:Unified documentation style. · InsightSoftwareConsortium/ITK@e9c0020 · GitHub](https://github.com/InsightSoftwareConsortium/ITK/commit/e9c002026368eee503bc39d363b9c9344d587385)  
Here:  
[ITK/itkMapContainer.h at e9c002026368eee503bc39d363b9c9344d587385 · InsightSoftwareConsortium/ITK · GitHub](https://github.com/InsightSoftwareConsortium/ITK/blob/e9c002026368eee503bc39d363b9c9344d587385/Code/Common/itkMapContainer.h#L98-L102)

HTH, Niels

---

<div class="post-metadata">

### Author: ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)
#### Post date: [March 22, 2023, 6:10pm UTC](https://discourse.itk.org/t/trying-to-delete-object-with-non-zero-reference-count/5807/11 "2023-03-22T18:10:40Z")

</div>

Since it has a `New` macro and `Pointer` type, it should probably use constructor disallow macro. Anyone willing to submit a PR?
