# itk v5.0: dynamic cast in itkMacro.h fails on MacOs

**URL:** https://discourse.itk.org/t/itk-v5-0-dynamic-cast-in-itkmacro-h-fails-on-macos/2392
**Category:** Uncategorized
**Created:** [November 8, 2019, 4:01pm UTC](https://discourse.itk.org/t/itk-v5-0-dynamic-cast-in-itkmacro-h-fails-on-macos/2392 "2019-11-08T16:01:20Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![dyoll](https://discourse.itk.org/user_avatar/discourse.itk.org/dyoll/32/2311_2.png) [@dyoll](https://discourse.itk.org/u/dyoll)
#### Post date: [November 8, 2019, 4:01pm UTC](https://discourse.itk.org/t/itk-v5-0-dynamic-cast-in-itkmacro-h-fails-on-macos/2392/1 "2019-11-08T16:01:20Z")

</div>

Hi

I built ITK on my mac with default options (BUILD\_SHARED\_LIBS=ON).  
compiler is : Apple clang version 11.0.0 (clang-1100.0.33.12)

I build my application(s) with:  
SET(CMAKE\_CXX\_STANDARD 11)  
SET(CMAKE\_CXX\_STANDARD\_REQUIRED ON)  
SET(CMAKE\_CXX\_EXTENSIONS OFF)  
SET(CMAKE\_CXX\_FLAGS “${CMAKE\_CXX\_FLAGS} -Wno-inconsistent-missing-override -Wunreachable-code-aggressive”)

It compiles fine, but at runtime at some point I get following exception:

itk::ExceptionObject: /Users/lloyd/Code/pack/InsightToolkit-5.0.0/source/Modules/Core/Common/include/itkMacro.h:1356:  
itk::ERROR: Failed dynamic cast to PKN3itk5ImageIfLj2EEE object type = Image

It is as if the itk::Image\<T,3\> has different type across library boundaries.  
I don’t really understand it - I am more experienced on Windows and don’t have any issues there with MSVC.

The “application” is actually a (boost unit) test runner console application, which loads test suite shared libs at runtime (testsuite plugins). Before upgrading to ITK v5.0.x I did not have this issue.

Any ideas?

Thanks, Bryn

---

<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: [November 8, 2019, 4:38pm UTC](https://discourse.itk.org/t/itk-v5-0-dynamic-cast-in-itkmacro-h-fails-on-macos/2392/2 "2019-11-08T16:38:13Z")

</div>

This seems like the same [issue](https://github.com/BlueQuartzSoftware/DREAM3D/issues/910) DREAM3D was having.

@dyoll We could not find the root cause of that problem, so @imikejackson worked around the problem.

---

<div class="post-metadata">

### Author: ![dyoll](https://discourse.itk.org/user_avatar/discourse.itk.org/dyoll/32/2311_2.png) [@dyoll](https://discourse.itk.org/u/dyoll)
#### Post date: [November 8, 2019, 4:39pm UTC](https://discourse.itk.org/t/itk-v5-0-dynamic-cast-in-itkmacro-h-fails-on-macos/2392/3 "2019-11-08T16:39:37Z")

</div>

I just realized something.

If I have only one testsuite plugin, the test runner can execute correctly. The error appears when I load a second plugin, which also depends on ITK.

---

<div class="post-metadata">

### Author: ![dyoll](https://discourse.itk.org/user_avatar/discourse.itk.org/dyoll/32/2311_2.png) [@dyoll](https://discourse.itk.org/u/dyoll)
#### Post date: [November 8, 2019, 4:48pm UTC](https://discourse.itk.org/t/itk-v5-0-dynamic-cast-in-itkmacro-h-fails-on-macos/2392/4 "2019-11-08T16:48:25Z")

</div>

Yes, indeed. It seems to be a similar issue.

I my case I use the itkSliceContiguousImage: [https://github.com/ITISFoundation/osparc-iseg/blob/master/Thirdparty/IJ/AlternativeMemoryModels/itkSliceContiguousImage.h](https://github.com/ITISFoundation/osparc-iseg/blob/master/Thirdparty/IJ/AlternativeMemoryModels/itkSliceContiguousImage.h)

The same setup works with MSVC and gcc on linux. I haven’t yet observed any issues for the main application. For the testsuites I can modify the testrunner to load a specific plugin, and let cmake call each one at a time. That should be a workaround, although I don’t like not understanding what causes the problem.

---

<div class="post-metadata">

### Author: ![imikejackson](https://discourse.itk.org/user_avatar/discourse.itk.org/imikejackson/32/35_2.png) [@imikejackson](https://discourse.itk.org/u/imikejackson)
#### Post date: [November 8, 2019, 5:10pm UTC](https://discourse.itk.org/t/itk-v5-0-dynamic-cast-in-itkmacro-h-fails-on-macos/2392/5 "2019-11-08T17:10:48Z")

</div>

DREAM.3D ([http://www.github.com/bluequartzsoftware/dream3d](http://www.github.com/bluequartzsoftware/dream3d)) has the exact same issue. We have plugins to the GUI app that some depend on ITK. The FIRST plugin that gets loaded will work correctly. All subsequent plugins that get loaded and that depend on ITK will not work. It is the casting issue.

We solved it by using the simplITKExplicit library which we liberally ripped out of SIMPLItk and now build as part of our base compilation ONLY if we are on ITK 5 and macOS. MSVC and gcc don’t need the ITKExplicit library and so we don’t compile it on those platforms.

[https://github.com/BlueQuartzSoftware/SIMPL/tree/develop/Source/SimpleITKExplicit](https://github.com/BlueQuartzSoftware/SIMPL/tree/develop/Source/SimpleITKExplicit)

That folder has the library. Feel free to grab those bits if you need it.

To be explicitly clear how this works (and the _ **only** _ we we got it to work) is the following;

Say we have 2 plugins APlug and BPlug. The dependency tree is this:

APlug-\>SIMPLib-\>ITKExplicit

BPlug-\>SIMPLib-\>ITKExplicit

You _ **cannot** _ just have APLug and BPlug link directly to ITKExplicit. For our project there must be an intervening common library that both APlug and BPlug link against (SIMPLib in our case).

Hope that helps.

---

<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: [November 8, 2019, 5:50pm UTC](https://discourse.itk.org/t/itk-v5-0-dynamic-cast-in-itkmacro-h-fails-on-macos/2392/6 "2019-11-08T17:50:49Z")

</div>

When we encounter this problem before I created a mini-project to experiment with the behavior of `dynamic_cast` on OSX. It may be useful for running experiments understanding the problem and the current behavior:

> **[blowekamp/PrivateDynamicCastTest](https://github.com/blowekamp/PrivateDynamicCastTest)**
>
> This is a little project to test the behavior of casting hidden symbols between separate binaries. - blowekamp/PrivateDynamicCastTest

I suspect the case where the `dynamic_cast` created a system log event, may now fail.

---

<div class="post-metadata">

### Author: ![matt.mccormick](https://discourse.itk.org/user_avatar/discourse.itk.org/matt.mccormick/32/7_2.png) [@matt.mccormick](https://discourse.itk.org/u/matt.mccormick)
#### Post date: [November 8, 2019, 8:15pm UTC](https://discourse.itk.org/t/itk-v5-0-dynamic-cast-in-itkmacro-h-fails-on-macos/2392/7 "2019-11-08T20:15:11Z")

</div>

> [@dyoll](#):
>
> I my case I use the itkSliceContiguousImage: [osparc-iseg/Thirdparty/IJ/AlternativeMemoryModels/itkSliceContiguousImage.h at master · ITISFoundation/osparc-iseg · GitHub](https://github.com/ITISFoundation/osparc-iseg/blob/master/Thirdparty/IJ/AlternativeMemoryModels/itkSliceContiguousImage.h)

This line:

> <https://github.com/ITISFoundation/osparc-iseg/blob/1a5bb965216e02979b939042db85bf053e74a115/Thirdparty/IJ/AlternativeMemoryModels/itkSliceContiguousImage.h#L54>

needs to be

```auto
class ITK_TEMPLATE_EXPORT SliceContiguousImage : 

```

`ITK_TEMPLATE_EXPORT` is required for macOS. Other platforms will not have an issue resolving the symbol correctly.

---

<div class="post-metadata">

### Author: ![dyoll](https://discourse.itk.org/user_avatar/discourse.itk.org/dyoll/32/2311_2.png) [@dyoll](https://discourse.itk.org/u/dyoll)
#### Post date: [November 8, 2019, 9:00pm UTC](https://discourse.itk.org/t/itk-v5-0-dynamic-cast-in-itkmacro-h-fails-on-macos/2392/8 "2019-11-08T21:00:01Z")

</div>

Thanks everybody for your suggestions.

ITK\_TEMPLATE\_EXPORT does change the error. Now I get various problems, e.g. in image iterators the region is empty:

tk::ExceptionObject: /Users/lloyd/Code/pack/InsightToolkit-5.0.0/source/Modules/Core/Common/include/itkImageConstIterator.h:210:  
itk::ERROR: Region ImageRegion (0x700003943868)  
Dimension: 3  
Index: [0, 0, 0]  
Size: [512, 512, 1]  
is outside of buffered region ImageRegion (0x7f9b7e436188)  
Dimension: 3  
Index: [0, 0, 0]  
Size: [0, 0, 0]

I will investigate further, but it seems the plugin phenomenon persists. The first loaded plugin is ok, but subsequent ones behave incorrectly. I will try the suggestion from @imikejackson.

---

<div class="post-metadata">

### Author: ![dyoll](https://discourse.itk.org/user_avatar/discourse.itk.org/dyoll/32/2311_2.png) [@dyoll](https://discourse.itk.org/u/dyoll)
#### Post date: [November 11, 2019, 7:39am UTC](https://discourse.itk.org/t/itk-v5-0-dynamic-cast-in-itkmacro-h-fails-on-macos/2392/9 "2019-11-11T07:39:37Z")

</div>

I found a different solution:

Before I loaded the plugins like this:

`dlopen(file_name.c_str(), RTLD_LOCAL | RTLD_LAZY);`

Now I load the plugins like this:

`dlopen(file_name.c_str(), RTLD_GLOBAL | RTLD_LAZY);`

According to the documentation, when you use RTLD\_LOCAL (the default):

> symbols defined in this library are not made available to resolve references in subsequently loaded libraries.

BTW: I followed @matt.mccormick and use ITK\_TEMPLATE\_EXPORT, instead of ITK\_EXPORT.

@imikejackson I believe the equivalent to RTLD\_GLOBAL for QPluginLoader is: `pluginLoader.setLoadHints(QLibrary::ExportExternalSymbolsHint);`

---

<div class="post-metadata">

### Author: ![imikejackson](https://discourse.itk.org/user_avatar/discourse.itk.org/imikejackson/32/35_2.png) [@imikejackson](https://discourse.itk.org/u/imikejackson)
#### Post date: [November 11, 2019, 2:49pm UTC](https://discourse.itk.org/t/itk-v5-0-dynamic-cast-in-itkmacro-h-fails-on-macos/2392/10 "2019-11-11T14:49:00Z")

</div>

I use Qt’s plugin loading APIs. I would have to dig into those APIs/Source to see how they load the plugin files.
