# VTK Crash on startup with CLR

**URL:** <https://discourse.vtk.org/t/vtk-crash-on-startup-with-clr/4507>\
**Category:** Development\
**Created:** [October 28, 2020, 3:33pm UTC](https://discourse.vtk.org/t/vtk-crash-on-startup-with-clr/4507 "2020-10-28T15:33:49Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![RKeene](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/r/bb73d2/32.png) [@RKeene](https://discourse.vtk.org/u/RKeene)\
**Post date:** [October 28, 2020, 3:33pm UTC](https://discourse.vtk.org/t/vtk-crash-on-startup-with-clr/4507/1 "2020-10-28T15:33:49Z")

</div>

I have a Visual C++ program, Visual Studio 2019, WinForms and am trying to add in VTK. I have two projects. One is the CLR project that does the WinForm UI called ORIScanUI, and a second project that does the unmanaged code (VTK, VC, PCL) is called ORIScan. I have VTK 9.0.1 source and it builds fine with CMake making the visual studio project and then Visual studio builds the VTK binaries. All the include paths in my project are correct and linking in the .lib and finding the .dlls works fine.

It crashes on startup before any windows show up on the screen.

Exception thrown at 0x00007FF9E8214350 (ntdll.dll) in ORIScanUI.exe: 0xC0000005: Access violation reading location 0xFFFFFFFFFFFFFFFF.

And the stack is  
ntdll.dll!RtlValidateHeap() Unknown  
KernelBase.dll!HeapValidate() Unknown

> ucrtbased.dll!\_CrtIsValidHeapPointer(const void \* block) Line 1407 C++  
> ucrtbased.dll!\_msize\_dbg(void \* block, int block\_use) Line 1059 C++  
> ucrtbased.dll!\_recalloc\_dbg(void \* block, unsigned \_\_int64 count, unsigned \_\_int64 element\_size, int block\_use, const char \* file\_name, int line\_number) Line 784 C++  
> ucrtbased.dll!\_register\_onexit\_function::\_\_l2::() Line 112 C++  
> ucrtbased.dll!\_\_crt\_seh\_guarded\_call::operator()\<void (void),int (void) &,void (void)\>(\_\_acrt\_lock\_and\_call::\_\_l2::void (void) && setup, \_register\_onexit\_function::\_\_l2::int (void) & action, \_\_acrt\_lock\_and\_call::\_\_l2::void (void) && cleanup) Line 204 C++  
> ucrtbased.dll!\_\_acrt\_lock\_and\_call\<int (void)\>(const \_\_acrt\_lock\_id lock\_id, \_register\_onexit\_function::\_\_l2::int (void) && action) Line 975 C++  
> ucrtbased.dll!\_register\_onexit\_function(\_onexit\_table\_t \* table, int(_)() function) Line 149 C++  
> ORIScanUI.exe!\_onexit(int(_)() function) Line 267 C++  
> ORIScanUI.exe!atexit(void(_)() function) Line 275 C++  
> ORIScanUI.exe!`dynamic initializer for ‘vtkDebugLeaksManagerInstance’'() Line 46 C++  
> [Managed to Native Transition]   
> ORIScanUI.exe!\_initterm(void (\_\_cdecl_)()\* pfbegin, void (\_\_cdecl\*)()\* pfend) Line 127 C++  
> ORIScanUI.exe!::LanguageSupport::InitializeNative() Line 360 C++  
> ORIScanUI.exe!::LanguageSupport::\_Initialize() Line 599 C++  
> ORIScanUI.exe!::LanguageSupport::Initialize() Line 806 C++  
> ORIScanUI.exe!.cctor() Line 857 C++  
> [Native to Managed Transition]   
> [Managed to Native Transition]   
> [Native to Managed Transition]   
> mscoreei.dll!\_CorExeMain() Unknown  
> mscoree.dll!\_CorExeMain\_Exported() Unknown  
> kernel32.dll!BaseThreadInitThunk() Unknown  
> ntdll.dll!RtlUserThreadStart() Unknown

It appears the problem is in vtkDebugLeaksManagerInstance  
My minimal test code does not even do anything. Simply including the  
#include “vtkClipClosedSurface.h”  
all by its self causes the crash. I assume VTK is starting up static functions and that causes the crash.  
If I make a super simple Visual C++ console app so there is no CLR and no managed code, it starts up fine.

Any ideas?
