Crash on cleanup after reading Step file

I'm using the current master branch (d419776). After the fixes with Jemalloc and missing headers I am now able to build correctly.

Running my application now mostly works, except for it crashing when it exits.

I'm getting an assertion error on the Standard_Type destructor:

Standard_Type::~Standard_Type ()
{
  // remove descriptor from the registry
  registry_type& aRegistry = GetRegistry();
  Standard_ASSERT(!aRegistry.UnBind(mySystemName), "Standard_Type::~Standard_Type() cannot find itself in registry",);
}

This is the call stack at this point:

    TKernel.dll!Standard_Type::~Standard_Type() Line 151    C++
    TKernel.dll!Standard_Type::`vector deleting destructor'(unsigned int)   C++
    TKernel.dll!Standard_Transient::Delete() Line 111   C++
    TKDESTEP.dll!opencascade::handle<Standard_Type>::EndScope() Line 386    C++
    TKDESTEP.dll!opencascade::handle<Standard_Type>::~handle<Standard_Type>() Line 82   C++
    TKDESTEP.dll!`HeaderSection_FileSchema::get_type_descriptor'::`2'::`dynamic atexit destructor for 'THE_TYPE_INSTANCE''()    C++
    TKDESTEP.dll!__scrt_dllmain_uninitialize_c() Line 398   C++
    TKDESTEP.dll!dllmain_crt_process_detach(const bool is_terminating) Line 182 C++
    TKDESTEP.dll!dllmain_crt_dispatch(HINSTANCE__ * const instance, const unsigned long reason, void * const reserved) Line 220 C++
    TKDESTEP.dll!dllmain_dispatch(HINSTANCE__ * const instance, const unsigned long reason, void * const reserved) Line 293 C++
    TKDESTEP.dll!_DllMainCRTStartup(HINSTANCE__ * const instance, const unsigned long reason, void * const reserved) Line 335   C++

I'm reading a Step file in this way, using OCCT as shared libraries:

Handle(TDocStd_Application) _xdeApp = new TDocStd_Application();
BinXCAFDrivers::DefineFormat(_xdeApp);
Handle(TDocStd_Document) _doc;

try
{
    _xdeApp->NewDocument(TCollection_ExtendedString("BinXCAF"), _doc);

    STEPCAFControl_Reader reader;
    reader.SetColorMode(true);
    reader.SetNameMode(true);
    reader.SetLayerMode(true);

    if (reader.ReadFile(stepPath.ToCString()) != IFSelect_RetDone)
    {
        std::string errStr = (TCollection_AsciiString("Error reading file: ") + stepPath).ToCString();
        throw std::exception(errStr.c_str());
    }

    if (!reader.Transfer(_doc))
    {
        std::string errStr = (TCollection_AsciiString("Error transferring file: ") + stepPath).ToCString();
        throw std::exception(errStr.c_str());
    }

This looks similar to the problem described here, although that one was marked as solved.

Dmitrii Pasukhin's picture

Hello. You are having a crash or static assert? I mean exe return code is 0 or there are some error code with exception.

I was multiple times trying to reproduce, but without success.

Best regards, Dmitrii.

Elias Cohenca's picture

See attached image. When running Debug configuration from the IDE it crashes when the program ends.

It's enough to just add a function with:

STEPCAFControl_Reader reader;

and it should reproduce. The only way to continue is to abort, which ends with "exited with code 3 (0x3)."

Attachments: 
Dmitrii Pasukhin's picture

Thank you! What about release? It have crash?

Best regard, Dmitrii.

Elias Cohenca's picture

No crash on release, but maybe it's because I have:
BUILD_RELEASE_DISABLE_EXCEPTIONS ON
?

Dmitrii Pasukhin's picture

Static assert only for debug. It will not affect the release. And origin issue right now have a protecting from crash.

I keep static assert to find memory leak. Before fix probably here was a crash. But it is always great to find an issue and fix it. Thank you for your research! I will try to help

Best regards, Dmitrii.

Elias Cohenca's picture

Thanks Dmitrii.

Yes, I'm aware that the assert won't appear on release, but I thought it's worth reporting because this was supposedly fixed and it may be signaling another underlying problem.

Dmitrii Pasukhin's picture

To be honest. It is much better then I expect. I was misleaded the boolean conditionals.

I inversed the conditions. My apologies. Need to not use "!". Why we didn't find it - we are using TCL based applications. And they are poor organized and just destroy stack :) They are not calling any destructors.

But it much better then have a bug :)

Thank you a lot!

Dmitrii Pasukhin's picture

Additionally. Could you please share you VS's linker libs list (with their order). I will try to create similar project.

Best regards, Dmitrii.

Elias Cohenca's picture

Here's my libs:

TKernel.lib
TKLCAF.lib
TKDESTEP.lib
TKBinXCAF.lib
TKXCAF.lib
TKMath.lib
TKBRep.lib
TKV3d.lib
TKMesh.lib
TKRWMesh.lib
TKDEGLTF.lib
Dmitrii Pasukhin's picture

Thank you. I was be able to reproduce. 

Dmitrii Pasukhin's picture