# Segmentation Fault style error during Casting

**URL:** <https://discourse.itk.org/t/segmentation-fault-style-error-during-casting/485>\
**Category:** Algorithms\
**Created:** [December 5, 2017, 4:04pm UTC](https://discourse.itk.org/t/segmentation-fault-style-error-during-casting/485 "2017-12-05T16:04:17Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![WillScottJackson](https://discourse.itk.org/user_avatar/discourse.itk.org/willscottjackson/32/190_2.png) [@WillScottJackson](https://discourse.itk.org/u/WillScottJackson)\
**Post date:** [December 5, 2017, 4:04pm UTC](https://discourse.itk.org/t/segmentation-fault-style-error-during-casting/485/1 "2017-12-05T16:04:17Z")

</div>

Hi there

I’m debugging some code to perform a process on 3D MRI data and I’m getting hung up on a crash I keep encountering when my application is performing a type cast from an unsigned short image to a float.

The relevant snippet of code is as follows:  
indent preformatted text by 4 spaces  
typedef unsigned short InputPixelType;  
typedef float OutputPixelType;

const unsigned int dims = 3;

typedef itk::Image\<InputPixelType, dims\> InputImageType;  
typedef itk::Image\<OutputPixelType, dims\> OutputImageType;

// Import the image data from pointer  
typedef itk::ImportImageFilter\<InputPixelType, dims\> ImportFilterType;  
ImportFilterType::Pointer importFilter = ImportFilterType::New();

// Set up region, spacing, origin, direction etc… (not included to save space)

// m\_pSrc is an unsigned short pointer to image data  
// m\_size is the size (in pixels) of the image dataset  
const bool LetImageContainerManageMemory = true;  
importFilter-\>SetImportPointer(m\_pSrc,m\_size, LetImageContainerManageMemory);

importFilter-\>Update();

// Now cast the image  
typedef itk::CastImageFilter\<InputImageType, OutputImageType\> CastFilterType;  
CastFilterType::Pointer castFilter = CastFilterType::New();  
castFilter-\>SetInput(importFilter-\>GetOutput());  
castFilter-\>Update();

```
 indent preformatted text by 4 spaces

```

Upon calling castFilter-\>Update(), I get a crash which looks like a segmentation fault type of crash. (in reality, I have the Update() call in a try-catch block); does anyone have any suggestions as to why this may be occurring? Are there extra steps I can take to ensure the cast performs safely?

This may be relevant but this algorithm is being performed in a separate thread; I don’t know how well ITK calls handle being placed into worker threads.

Thank you!

---

<div class="post-metadata">

**Author:** ![tim-evain](https://discourse.itk.org/user_avatar/discourse.itk.org/tim-evain/32/220_2.png) [@tim-evain](https://discourse.itk.org/u/tim-evain)\
**Post date:** [December 5, 2017, 4:10pm UTC](https://discourse.itk.org/t/segmentation-fault-style-error-during-casting/485/2 "2017-12-05T16:10:51Z")

</div>

Hi @WillScottJackson,

> [@WillScottJackson](#):
>
> typedef itk::Image\<ImagePixelType, dims\> OutputImageType;

I don’t see any definition of ImagePixelType in your snippet. Maybe you rather wanted to put OutputPixelType?

Tim

---

<div class="post-metadata">

**Author:** ![WillScottJackson](https://discourse.itk.org/user_avatar/discourse.itk.org/willscottjackson/32/190_2.png) [@WillScottJackson](https://discourse.itk.org/u/WillScottJackson)\
**Post date:** [December 5, 2017, 4:15pm UTC](https://discourse.itk.org/t/segmentation-fault-style-error-during-casting/485/3 "2017-12-05T16:15:48Z")

</div>

Ah yes, thank you! Have edited accordingly!

---

<div class="post-metadata">

**Author:** ![tim-evain](https://discourse.itk.org/user_avatar/discourse.itk.org/tim-evain/32/220_2.png) [@tim-evain](https://discourse.itk.org/u/tim-evain)\
**Post date:** [December 5, 2017, 4:23pm UTC](https://discourse.itk.org/t/segmentation-fault-style-error-during-casting/485/4 "2017-12-05T16:23:26Z")

</div>

Is the caught error of type ExceptionObject ? If so, what does it say ?

---

<div class="post-metadata">

**Author:** ![WillScottJackson](https://discourse.itk.org/user_avatar/discourse.itk.org/willscottjackson/32/190_2.png) [@WillScottJackson](https://discourse.itk.org/u/WillScottJackson)\
**Post date:** [December 5, 2017, 4:26pm UTC](https://discourse.itk.org/t/segmentation-fault-style-error-during-casting/485/5 "2017-12-05T16:26:59Z")

</div>

It doesn’t catch any error. Instead I get hard crash with no information being printed to my console.

try {  
castFilter-\>Update();  
} catch (itk::ExceptionObject & err) {  
std::cout \<\< “ExceptionObject Caught!” \<\< std::endl;  
std::cout \<\< err \<\< std::endl;  
return false;  
}

It leaves me to suspect something goes wrong inside Update() which kills the application rather than any error handling catching it and reporting it back to my exception handler.

---

<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:** [December 5, 2017, 4:28pm UTC](https://discourse.itk.org/t/segmentation-fault-style-error-during-casting/485/6 "2017-12-05T16:28:58Z")

</div>

Which version of ITK are you using? Some stable release or a recent master version? Have you tried stepping through it with a debugger to see where the error is happening?

---

<div class="post-metadata">

**Author:** ![WillScottJackson](https://discourse.itk.org/user_avatar/discourse.itk.org/willscottjackson/32/190_2.png) [@WillScottJackson](https://discourse.itk.org/u/WillScottJackson)\
**Post date:** [December 5, 2017, 4:32pm UTC](https://discourse.itk.org/t/segmentation-fault-style-error-during-casting/485/7 "2017-12-05T16:32:45Z")

</div>

It is fairly old, version 4.7 is the one I’m working with.

I’m not having the easiest time debugging as the function I’m working with is being built into a .dll file and running through a GUI front end.

(I’m debugging an existing code base rather than writing it myself from scratch, hence the awkward set up)

---

<div class="post-metadata">

**Author:** ![WillScottJackson](https://discourse.itk.org/user_avatar/discourse.itk.org/willscottjackson/32/190_2.png) [@WillScottJackson](https://discourse.itk.org/u/WillScottJackson)\
**Post date:** [December 5, 2017, 4:42pm UTC](https://discourse.itk.org/t/segmentation-fault-style-error-during-casting/485/8 "2017-12-05T16:42:03Z")

</div>

I’ve even done some couts just to see what happens:

```
std::cout << "Type Cast Set up" << std::endl;

try {
	castFilter->Update();
} catch (itk::ExceptionObject & err) {
	std::cout << "ExceptionObject Caught!" << std::endl;
	std::cout << err << std::endl;
	return false;
}

std::cout << "Type Cast Successful" << std::endl;

```

And the console returns this:  
Type Cast Set up  
The program ‘[5644] TestViewer.vshost.exe: Managed’ has exited with code -529697949 (0xe06d7363).

As the test application has crashed and has to close.

---

<div class="post-metadata">

**Author:** ![tim-evain](https://discourse.itk.org/user_avatar/discourse.itk.org/tim-evain/32/220_2.png) [@tim-evain](https://discourse.itk.org/u/tim-evain)\
**Post date:** [December 5, 2017, 6:33pm UTC](https://discourse.itk.org/t/segmentation-fault-style-error-during-casting/485/9 "2017-12-05T18:33:40Z")

</div>

Is the error can be caught at all with:  
try  
{ castFilter-\>Update() }  
catch (…)  
{ std::cout\<\<“Something was caught” }  
?

If so, the error should be trackable in the code. But anyhow I don’t know if you can do so without access to debugging.

---

<div class="post-metadata">

**Author:** ![WillScottJackson](https://discourse.itk.org/user_avatar/discourse.itk.org/willscottjackson/32/190_2.png) [@WillScottJackson](https://discourse.itk.org/u/WillScottJackson)\
**Post date:** [December 6, 2017, 11:45am UTC](https://discourse.itk.org/t/segmentation-fault-style-error-during-casting/485/10 "2017-12-06T11:45:10Z")

</div>

I managed to catch the exception, I got thrown:  
Access Violation - no RTTI Data!

Although the standard itk::ExceptionObject didn’t work.

---

<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:** [December 6, 2017, 1:45pm UTC](https://discourse.itk.org/t/segmentation-fault-style-error-during-casting/485/11 "2017-12-06T13:45:42Z")

</div>

Perhaps you have multiple versions of ITK on your system? This could be a sign that you are including one version but linking ( runtime or compile ) to another.

---

<div class="post-metadata">

**Author:** ![WillScottJackson](https://discourse.itk.org/user_avatar/discourse.itk.org/willscottjackson/32/190_2.png) [@WillScottJackson](https://discourse.itk.org/u/WillScottJackson)\
**Post date:** [December 6, 2017, 4:05pm UTC](https://discourse.itk.org/t/segmentation-fault-style-error-during-casting/485/12 "2017-12-06T16:05:58Z")

</div>

I’ve been crawling over my Visual Studio set up. As far as I can tell, everything is correctly referenced. I am building with 4.7 libraries and am linking with 4.7 libraries. The only thing I can think of at the moment is maybe the initial build had some options unchecked when building with CMake.
