# Foundation.CreateDynamicThresholdImage related memory leak

**URL:** <https://forum.commonvisionblox.com/t/foundation-createdynamicthresholdimage-related-memory-leak/846>\
**Category:** C-style API\
**Tags:** csharp, foundation\
**Created:** [August 6, 2019, 11:01am UTC](https://forum.commonvisionblox.com/t/foundation-createdynamicthresholdimage-related-memory-leak/846 "2019-08-06T11:01:02Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Foster](https://avatars.discourse-cdn.com/v4/letter/f/aca169/32.png) [@Foster](https://forum.commonvisionblox.com/u/Foster)\
**Post date:** [August 6, 2019, 11:01am UTC](https://forum.commonvisionblox.com/t/foundation-createdynamicthresholdimage-related-memory-leak/846/1 "2019-08-06T11:01:02Z")

</div>

Hi there,  
I’m using the Foundation.CreateDynamicThesholdImage to pre-process an image before being passed through to the FBlob function.

Previously, I was doing simple thresholding all worked well. Since implementing Foundation.CreateDynamicThresholdImage, I see leaking memory issues. Removing the call removes the leak.

I’ve attached a simplified overview of the function that uses this method, below (hopefully the formatting will not go awry!);

```auto
if (Cvb.Image.IsImage((Cvb.Image.IMG)RawImage)) {
   // 1. Create temporary Image to run through dynamic thresholding before passing to FBlob...
   Image.IMG _imageForThresholding = new Image.IMG((Image.IMG)RawImage);               
   Foundation.CreateDynamicThresholdImage(_imageForThresholding, _maskSize, _threshold, out 
   Image.IMG _imageForBlobDetection);
			
   Foundation.FBLOB blob = Foundation.FBlobCreate(_imageForBlobDetection, 0);
            
   // 2. Look for blobs within a specific area (the ROI)...
   Foundation.FBlobSetArea(blob, 0, ROI.Left, ROI.Top, ROI.Right, ROI.Bottom);
 
   // 3. Look for black blobs on a white field, and with a specific black/white threshold...
   Foundation.FBlobSetObjectFeature(blob, _threshold, 
       (int)Foundation.BlobFeatureType.FBLOB_BLACK_TO_FEATURE);
 
   // 4. Run blob detection...
   Foundation.FBlobExec(blob);

   ...doing something useful with the blobs found...
 
   Image.ReleaseObj(blob);
}

```

This method is call continuously so that we can track objects within a live stream.

The garbage collector does not seem to handle the disposing of any resource associated with calls to CreateDynamicThresholdImage.

Is there a requirement for me to call something similar to the ReleaseObj used for the blob object? I can’t seem to find anything that does this job.

Any help would be much appreciated.

Cheers,  
Lee

---

<div class="post-metadata">

**Author:** ![parsd](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.commonvisionblox.com/parsd/32/7_2.png) [@parsd](https://forum.commonvisionblox.com/u/parsd)\
**Post date:** [August 6, 2019, 11:47am UTC](https://forum.commonvisionblox.com/t/foundation-createdynamicthresholdimage-related-memory-leak/846/2 "2019-08-06T11:47:11Z")

</div>

Hi @Foster,

first I would propose using the new _Stemmer.Cvb_ and _Stemmer.Cvb.Foundation_ wrappers which are shipped since :cvb: 2019 (13.02.000):

```auto
BlobResult[] FindBlobs(Image rawImage, Rect aoi, int maskSize, int threshold)
{
  if (rawImage == null)
    throw new ArgumentNullException(nameof(rawImage));

  using (var aoiImage = rawImage.Map(aoi))
  {
    using (var binarized = Threshold.Dynamic(aoiImage, maskSize, threshold))
    {
      return Blob.SearchAll(binarized);
    }
  }
}

```

For this code to work you need:

```auto
using Stemmer.Cvb;
using Stemmer.Cvb.Foundation;

```

Addendum: And if you want to find black blobs instead of white ones replace `SearchAll` with:

```auto
return Blob.BinarizeAndSearchAll(binarized.Planes[0], 0, 128);

```

The key issue with both source codes is the life-time management of the `Image`s/`IMG`s. With the new wrappers the `Image` can be properly garbage collected as it is a ‘proper’ .Net object, but you will run out of memory nevertheless if the calls to this method happen to frequently. Thus I managed the life-time of the temporaries via `using`.

In your source code you also need to release the `_imageForBlobDetection` temporary via `Image.ReleaseObj(_imageForBlobDetection);`. `IMG`s are essentially just `IntPtr`s which point to a native `IImageVpa` object. And for these you must manually call `ReleaseObj` as otherwise you leak memory.

---

<div class="post-metadata">

**Author:** ![Foster](https://avatars.discourse-cdn.com/v4/letter/f/aca169/32.png) [@Foster](https://forum.commonvisionblox.com/u/Foster)\
**Post date:** [August 6, 2019, 12:14pm UTC](https://forum.commonvisionblox.com/t/foundation-createdynamicthresholdimage-related-memory-leak/846/3 "2019-08-06T12:14:27Z")

</div>

Hi @parsd,  
Many thanks for your (very) quick response and snippet. I’m currently using 13.01.000 so will update to the suggested version to look at your suggested approach.

Best regards,  
Lee

---

<div class="post-metadata">

**Author:** ![Foster](https://avatars.discourse-cdn.com/v4/letter/f/aca169/32.png) [@Foster](https://forum.commonvisionblox.com/u/Foster)\
**Post date:** [August 6, 2019, 12:31pm UTC](https://forum.commonvisionblox.com/t/foundation-createdynamicthresholdimage-related-memory-leak/846/4 "2019-08-06T12:31:31Z")

</div>

Hi @parsd,  
Could you advise on where to access the download version 13.02.000? The website only appears to offer CVB 2018 [v13.01.006](https://www.commonvisionblox.com/en/download-cvb-windows-32-bit-and-64-bit/).

Thanks in advance,  
Lee

---

<div class="post-metadata">

**Author:** ![parsd](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.commonvisionblox.com/parsd/32/7_2.png) [@parsd](https://forum.commonvisionblox.com/u/parsd)\
**Post date:** [August 6, 2019, 12:33pm UTC](https://forum.commonvisionblox.com/t/foundation-createdynamicthresholdimage-related-memory-leak/846/5 "2019-08-06T12:33:03Z")

</div>

Sure, also here on the forum 😉:

[https://forum.commonvisionblox.com/c/downloads](https://forum.commonvisionblox.com/c/downloads)

---

<div class="post-metadata">

**Author:** ![Foster](https://avatars.discourse-cdn.com/v4/letter/f/aca169/32.png) [@Foster](https://forum.commonvisionblox.com/u/Foster)\
**Post date:** [August 6, 2019, 12:34pm UTC](https://forum.commonvisionblox.com/t/foundation-createdynamicthresholdimage-related-memory-leak/846/6 "2019-08-06T12:34:17Z")

</div>

Ah- missed that 🙂

Thanks!

---

<div class="post-metadata">

**Author:** ![parsd](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.commonvisionblox.com/parsd/32/7_2.png) [@parsd](https://forum.commonvisionblox.com/u/parsd)\
**Post date:** [August 6, 2019, 12:39pm UTC](https://forum.commonvisionblox.com/t/foundation-createdynamicthresholdimage-related-memory-leak/846/7 "2019-08-06T12:39:29Z")

</div>

You are welcome. The forum is the place where you get the most up-to-date information about :cvb:. But I cannot promise that we are always able to answer that fast 😄…

Questions and feedback are always welcome 👍

---

<div class="post-metadata">

**Author:** ![Foster](https://avatars.discourse-cdn.com/v4/letter/f/aca169/32.png) [@Foster](https://forum.commonvisionblox.com/u/Foster)\
**Post date:** [August 6, 2019, 12:46pm UTC](https://forum.commonvisionblox.com/t/foundation-createdynamicthresholdimage-related-memory-leak/846/8 "2019-08-06T12:46:48Z")

</div>

It’s a useful facility - and I don’t expect such a quick response all of the time 🙂

Just a FYI; it seems that the forum download facility does not like Chrome very much (repeatedly comes up with a ‘might be temporarily down or it may have moved permanently to a new web address.’ error). However, MS Edge works a treat…😛
