BRepExtrema_ExtPF is throwing an exception in Debug mode only

I am using BRepExtrema_ExtPF API (OCCT 7.6.0) in one of my method mentioned in attached method.cpp file.
It is working without any issue in Visual studio 2017 Release (x64) configuration. But it is throwing an exception in Debug(x64) configuration.

Attached files :
method.cpp : method implementation
Exception : _CrtIsValidHeapPointer(block) --> thrown-exception.png
Call stack : CallStack.png

Can you help me to solve this issue?
Thanks in advance.

Mikhail Sazonov's picture

Such exception can raise if you mix debug and release mode DLLs.

Vinojan Thiyagarasah's picture

For, OCCT I don't use separate DLLs for Release and Debug modes. I only used the release mode built OCCT DLLs.
And my program worked without any issue with OCCT 7.5.0.

Kirill Gavrilov's picture

Mixture of Release/Debug MSVCRT (C/C++ runtime libraries) is fundamental issue of Visual Studio compilers (there are no such problems with GCC on Linux, for example), so that you never know when it might strike you. There are several techniques that still allows mixing Release/Debug runtimes, which OCCT implements, but without comprehensive testing it remains a fragile combination.

Your particular code snippet uses BRepExtrema_ExtPF / Extrema_GenExtPS classes allocated on the stack. OCCT 7.6.0 has a patch refactoring these classes towards avoiding redundant dynamic memory allocations of arrays. As result, some NCollection_Array2 that were previously allocated dynamically by Handles, now stored as simple class fields. Due to memory allocator interfaces of NCollection_Array1/NCollection_Array2 collections, current code leads to new/delete being called by different CRTs in your case.

To workaround this issue at OCCT level, it should be enough adding ~BRepExtrema_ExtPF / ~Extrema_GenExtPS destructors exported from library (implemented in .cxx, so that using the same CRT as the code allocating arrays), instead of automatically generated destructors currently used (arrays deletion is done by CRT used by Application, while allocation is done by CRT used by OCCT libraries).

Fill free to report a bug on OCCT Bugtracker and suggest patches, if it helps.

Vinojan Thiyagarasah's picture

Thank you Kirill, for your explanation. It helps me to understand the issue.