# Segmentation effects crashing Slicer

**URL:** <https://discourse.slicer.org/t/segmentation-effects-crashing-slicer/7474>\
**Category:** Support\
**Created:** [July 8, 2019, 10:26pm UTC](https://discourse.slicer.org/t/segmentation-effects-crashing-slicer/7474 "2019-07-08T22:26:01Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![smrolfe](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/smrolfe/32/3659_2.png) [@smrolfe](https://discourse.slicer.org/u/smrolfe)\
**Post date:** [July 8, 2019, 10:26pm UTC](https://discourse.slicer.org/t/segmentation-effects-crashing-slicer/7474/1 "2019-07-08T22:26:01Z")

</div>

Hello,  
I’m using the preview version of Slicer to do a segmentation on a 2.6 GB micro-CT scan saved as a TIF. I can do a simple segmentation, but when I try to apply the mask or split volume effect, the Slicer application crashes. I’ve checked the error logs and there were no errors generated. I’m using a Windows machine with 32GB RAM and ~500GB free disk space.

I also generated the following warnings when trying to use the islands effect:

> Slicer has caught an internal error.  
> You may be able to continue from this point, but results are undefined.  
> Suggested action is to save your work and restart.  
> If you have a repeatable sequence of steps that causes this message, please report the issue \> \> following instructions available at [http://slicer.org](http://slicer.org)  
> The message detail is:  
> Exception thrown in event: d:\d\p\slicer-0-build\itk\modules\core\common\include\itkImportImageContainer.hxx:199:  
> Failed to allocate memory for image.

and

> Slicer has caught an internal error.  
> You may be able to continue from this point, but results are undefined.  
> Suggested action is to save your work and restart.  
> If you have a repeatable sequence of steps that causes this message, please report the issue following instructions available at [http://slicer.org](http://slicer.org)  
> The message detail is:  
> Exception thrown in event: bad allocation

I expected the amount of memory to be sufficient, but could this be the problem? Thanks for your advice.

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [July 9, 2019, 3:34am UTC](https://discourse.slicer.org/t/segmentation-effects-crashing-slicer/7474/2 "2019-07-09T03:34:17Z")

</div>

It seems that you run out of memory. If you don’t expect this then maybe some extra memory allocations are performed or extents are computed incorrectly. You can add a breakpoint before itkImportImageContainer.hxx:199 and inspect variables to see if the memory that is attempted to be allocated have sensible size.

---

<div class="post-metadata">

**Author:** ![smrolfe](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/smrolfe/32/3659_2.png) [@smrolfe](https://discourse.slicer.org/u/smrolfe)\
**Post date:** [July 9, 2019, 11:03pm UTC](https://discourse.slicer.org/t/segmentation-effects-crashing-slicer/7474/3 "2019-07-09T23:03:24Z")

</div>

Thanks @lassoan, I’ll try this.

---

<div class="post-metadata">

**Author:** ![jcfr](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jcfr/32/17825_2.png) [@jcfr](https://discourse.slicer.org/u/jcfr)\
**Post date:** [July 30, 2019, 10:27pm UTC](https://discourse.slicer.org/t/segmentation-effects-crashing-slicer/7474/4 "2019-07-30T22:27:01Z")

</div>

After trying to load the dataset, I didn’t get a bad allocation. Instead I got a crash due to a lookup in scalar array using a negative index.

Code where the error occur is the following:

> <https://github.com/Kitware/VTK/blob/2cd7e84cb92a5bc091e2764bdfd35a01caafc47c/Imaging/Stencil/vtkImageToImageStencil.cxx#L146-L154>

The crash occur when executing the line `inScalars->GetComponent(idS++,0);` with `idS = -2147482744`

It turns out that the `idS` ivar is of type `int`, and the value overflow … it should be changed to `vtkIdType` along with all intermediate variables.

The following snippet allows to reproduce the incorrect index value:

```auto
  int extent_0 = 0;
  int extent_1 = 1207;
  int extent_2 = 0;
  int extent_3 = 1250;
  int extent_4 = 0;
  int idY = 47;
  int idZ = 1421;

  int idS_test = ((extent_1 - extent_0 + 1)*
                   ((extent_3 - extent_2 + 1)*(idZ - extent_4) +
                    (idY - extent_2)));
  std::cout << "idS_test " << idS_test << std::endl;

  idY = 48;

  idS_test = ((extent_1 - extent_0 + 1)*
                     ((extent_3 - extent_2 + 1)*(idZ - extent_4) +
                      (idY - extent_2)));
  std::cout << "idS_test " << idS_test << std::endl;

```

associated output is:

```auto
idS_test 2147483344
idS_test -2147482744

```

### Proposed path forward

To move forward,

We need to change the type of all intermediate variable to `vtkIdType`. The following work as expected:

```auto
  vtkIdType extent_0 = 0;
  vtkIdType extent_1 = 1207;
  vtkIdType extent_2 = 0;
  vtkIdType extent_3 = 1250;
  vtkIdType extent_4 = 0;
  vtkIdType idY = 47;
  vtkIdType idZ = 1421;

  vtkIdType idS_test = ((extent_1 - extent_0 + 1)*
                   ((extent_3 - extent_2 + 1)*(idZ - extent_4) +
                    (idY - extent_2)));
  std::cout << "idS_test " << idS_test << std::endl;

  idY = 48;

  idS_test = ((extent_1 - extent_0 + 1)*
                     ((extent_3 - extent_2 + 1)*(idZ - extent_4) +
                      (idY - extent_2)));
  std::cout << "idS_test " << idS_test << std::endl;

```

Output

```auto
idS_test 2147483344
idS_test 2147484552

```

---

<div class="post-metadata">

**Author:** ![jcfr](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jcfr/32/17825_2.png) [@jcfr](https://discourse.slicer.org/u/jcfr)\
**Post date:** [August 1, 2019, 5:51pm UTC](https://discourse.slicer.org/t/segmentation-effects-crashing-slicer/7474/5 "2019-08-01T17:51:44Z")

</div>

Here is the corresponding VTK merge request: [https://gitlab.kitware.com/vtk/vtk/merge\_requests/5816](https://gitlab.kitware.com/vtk/vtk/merge_requests/5816)

---

<div class="post-metadata">

**Author:** ![jcfr](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jcfr/32/17825_2.png) [@jcfr](https://discourse.slicer.org/u/jcfr)\
**Post date:** [August 1, 2019, 6:07pm UTC](https://discourse.slicer.org/t/segmentation-effects-crashing-slicer/7474/6 "2019-08-01T18:07:42Z")

</div>

Corresponding change has been integrated in Slicer as [r28419](http://viewvc.slicer.org/viewvc.cgi/Slicer4?view=revision&revision=28419)

---

<div class="post-metadata">

**Author:** ![smrolfe](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/smrolfe/32/3659_2.png) [@smrolfe](https://discourse.slicer.org/u/smrolfe)\
**Post date:** [August 2, 2019, 3:58pm UTC](https://discourse.slicer.org/t/segmentation-effects-crashing-slicer/7474/7 "2019-08-02T15:58:02Z")

</div>

The mask/split volume effects are working now for my \>2G images in the preview version. Thanks @jcfr!
