简体   繁体   中英

Crash when NXOpen objects are deleted in Unreal Engine

Hello stackoverflow community,

first time asking.

I'm working on a prototype for modeling CAD data in VR with the Unreal Engine. I have managed to get my Unreal project to build with my CAD System Siemens NX, or rather the API NXOpen.

Overall the API works and I can call NXOpen functions and can create basic objects But some operations cause crashes and my guess is that it has something to do with memory management. I cant wrap my head around what exactly is happening.

For example I have code that creates a NX block feature that works fine. Afterwards I'll need a body object that is stored inside the block feature. The access also works, but when 'bodies' runs out of scope, it crashes. Similar crashes happen when accessing all kinds of data within NX features. This dummy code triggers the crash quite often:


    // 'block' gets created here

    if (block) {
        std::vector<NXOpen::Body*> bodies = block->GetBodies();
        if (bodies.size() > 0) {
            NXOpen::Body* body = bodies.front();
        }
    } // <-- this line crashes

Exception thrown: read access violation.
**flag** was 0xFFFFFFFFFFFFFFFF. occurred

    [Inline Frame] UE4Editor-Core.dll!__TBB_machine_cmpswp1(volatile void *) Line 69    C++
>   [Inline Frame] UE4Editor-Core.dll!__TBB_TryLockByte(unsigned char &) Line 917   C++
    UE4Editor-Core.dll!__TBB_LockByte(unsigned char & flag) Line 924    C++
    [Inline Frame] UE4Editor-Core.dll!MallocMutex::scoped_lock::{ctor}(MallocMutex &) Line 66   C++
    UE4Editor-Core.dll!rml::internal::Block::freePublicObject(rml::internal::FreeObject * objectToFree) Line 1382   C++
    [Inline Frame] UE4Editor-Core.dll!rml::internal::internalPoolFree(rml::internal::MemoryPool * memPool, void *) Line 2571    C++
    UE4Editor-Core.dll!rml::internal::internalFree(void * object) Line 2595 C++
    UE4Editor-Core.dll!FMemory::Free(void * Original) Line 76   C++
    UE4Editor-GestEnUE-Win64-DebugGame.dll!operator delete(void * Ptr, unsigned __int64 Size) Line 6    C++
    [External Code] 
    UE4Editor-GestEnUE-Win64-DebugGame.dll!UNXConnection::AddPointSetsToBody(const FString & name) Line 268 C++
    UE4Editor-GestEnUE-Win64-DebugGame.dll!ANXPart::AddCylinder(FVector pos, FVector axis, float height, float diameter, bool WorldSpace) Line 103  C++
    UE4Editor-GestEnUE-Win64-DebugGame.dll!ANXPart::execAddCylinder(UObject * Context, FFrame & Stack, void * const Z_Param__Result) Line 18    C++
    UE4Editor-CoreUObject.dll!UFunction::Invoke(UObject * Obj, FFrame & Stack, void * const Z_Param__Result) Line 4643  C++
    UE4Editor-CoreUObject.dll!UObject::CallFunction(FFrame & Stack, void * const Z_Param__Result, UFunction * Function) Line 904    C++
    [Inline Frame] UE4Editor-CoreUObject.dll!FFrame::Step(UObject *) Line 319   C++
    UE4Editor-CoreUObject.dll!UObject::ProcessContextOpcode(FFrame & Stack, void * const Z_Param__Result, bool bCanFailSilently) Line 2314  C++
    [Inline Frame] UE4Editor-CoreUObject.dll!FFrame::Step(UObject *) Line 319   C++
    UE4Editor-CoreUObject.dll!UObject::execLetObj(UObject * Context, FFrame & Stack, void * const Z_Param__Result) Line 2136    C++
    [Inline Frame] UE4Editor-CoreUObject.dll!FFrame::Step(UObject *) Line 319   C++
    UE4Editor-CoreUObject.dll!ProcessLocalScriptFunction(UObject * Context, FFrame & Stack, void * const Z_Param__Result) Line 974  C++
    UE4Editor-CoreUObject.dll!ProcessScriptFunction<void (__cdecl*)(UObject *,FFrame &,void *)>(UObject * Context, UFunction * Function, FFrame & Stack, void * const Z_Param__Result, void(*)(UObject *, FFrame &, void *) ExecFtor) Line 809  C++
    UE4Editor-CoreUObject.dll!ProcessLocalFunction(UObject * Context, UFunction * Fn, FFrame & Stack, void * const Z_Param__Result) Line 1033   C++
    [Inline Frame] UE4Editor-CoreUObject.dll!FFrame::Step(UObject *) Line 319   C++
    UE4Editor-CoreUObject.dll!ProcessLocalScriptFunction(UObject * Context, FFrame & Stack, void * const Z_Param__Result) Line 974  C++
    UE4Editor-CoreUObject.dll!ProcessScriptFunction<void (__cdecl*)(UObject *,FFrame &,void *)>(UObject * Context, UFunction * Function, FFrame & Stack, void * const Z_Param__Result, void(*)(UObject *, FFrame &, void *) ExecFtor) Line 809  C++
    UE4Editor-CoreUObject.dll!ProcessLocalFunction(UObject * Context, UFunction * Fn, FFrame & Stack, void * const Z_Param__Result) Line 1033   C++
    [Inline Frame] UE4Editor-CoreUObject.dll!FFrame::Step(UObject *) Line 319   C++
    UE4Editor-CoreUObject.dll!UObject::ProcessContextOpcode(FFrame & Stack, void * const Z_Param__Result, bool bCanFailSilently) Line 2314  C++
    [Inline Frame] UE4Editor-CoreUObject.dll!FFrame::Step(UObject *) Line 319   C++
    UE4Editor-CoreUObject.dll!ProcessLocalScriptFunction(UObject * Context, FFrame & Stack, void * const Z_Param__Result) Line 974  C++
    UE4Editor-CoreUObject.dll!ProcessScriptFunction<void (__cdecl*)(UObject *,FFrame &,void *)>(UObject * Context, UFunction * Function, FFrame & Stack, void * const Z_Param__Result, void(*)(UObject *, FFrame &, void *) ExecFtor) Line 809  C++
    UE4Editor-CoreUObject.dll!ProcessLocalFunction(UObject * Context, UFunction * Fn, FFrame & Stack, void * const Z_Param__Result) Line 1033   C++
    [Inline Frame] UE4Editor-CoreUObject.dll!FFrame::Step(UObject *) Line 319   C++
    UE4Editor-CoreUObject.dll!ProcessLocalScriptFunction(UObject * Context, FFrame & Stack, void * const Z_Param__Result) Line 974  C++
    UE4Editor-CoreUObject.dll!UObject::ProcessInternal(UObject * Context, FFrame & Stack, void * const Z_Param__Result) Line 1058   C++
    UE4Editor-CoreUObject.dll!UFunction::Invoke(UObject * Obj, FFrame & Stack, void * const Z_Param__Result) Line 4643  C++
    UE4Editor-CoreUObject.dll!UObject::ProcessEvent(UFunction * Function, void * Parms) Line 1464   C++
    UE4Editor-Engine.dll!AActor::ProcessEvent(UFunction * Function, void * Parameters) Line 769 C++
    UE4Editor-GestEnUE-Win64-DebugGame.dll!ABuilderBase::commit() Line 37   C++
    UE4Editor-GestEnUE-Win64-DebugGame.dll!AGestEnLeapPawn::Tick(float DeltaTime) Line 138  C++
    UE4Editor-Engine.dll!AActor::TickActor(float DeltaSeconds, ELevelTick TickType, FActorTickFunction & ThisTickFunction) Line 970 C++
    UE4Editor-Engine.dll!FActorTickFunction::ExecuteTick(float DeltaTime, ELevelTick TickType, ENamedThreads::Type CurrentThread, const TRefCountPtr<FGraphEvent> & MyCompletionGraphEvent) Line 164    C++
    [Inline Frame] UE4Editor-Engine.dll!FTickFunctionTask::DoTask(ENamedThreads::Type) Line 285 C++
    UE4Editor-Engine.dll!TGraphTask<FTickFunctionTask>::ExecuteTask(TArray<FBaseGraphTask *,FDefaultAllocator> & NewTasks, ENamedThreads::Type CurrentThread) Line 842  C++
    [Inline Frame] UE4Editor-Core.dll!FBaseGraphTask::Execute(TArray<FBaseGraphTask *,FDefaultAllocator> & CurrentThread, ENamedThreads::Type) Line 511 C++
    UE4Editor-Core.dll!FNamedTaskThread::ProcessTasksNamedThread(int QueueIndex, bool bAllowStall) Line 686 C++
    UE4Editor-Core.dll!FNamedTaskThread::ProcessTasksUntilQuit(int QueueIndex) Line 582 C++
    [Inline Frame] UE4Editor-Core.dll!FTaskGraphImplementation::ProcessThreadUntilRequestReturn(ENamedThreads::Type) Line 1406  C++
    UE4Editor-Core.dll!FTaskGraphImplementation::WaitUntilTasksComplete(const TArray<TRefCountPtr<FGraphEvent>,TInlineAllocator<4,FDefaultAllocator> > & Tasks, ENamedThreads::Type CurrentThreadIfKnown) Line 1457 C++
    UE4Editor-Engine.dll!FTickTaskSequencer::ReleaseTickGroup(ETickingGroup WorldTickGroup, bool bBlockTillComplete) Line 557   C++
    UE4Editor-Engine.dll!FTickTaskManager::RunTickGroup(ETickingGroup Group, bool bBlockTillComplete) Line 1505 C++
    UE4Editor-Engine.dll!UWorld::RunTickGroup(ETickingGroup Group, bool bBlockTillComplete) Line 789    C++
    UE4Editor-Engine.dll!UWorld::Tick(ELevelTick TickType, float DeltaSeconds) Line 1568    C++
    UE4Editor-UnrealEd.dll!UEditorEngine::Tick(float DeltaSeconds, bool bIdleMode) Line 1618    C++
    UE4Editor-UnrealEd.dll!UUnrealEdEngine::Tick(float DeltaSeconds, bool bIdleMode) Line 403   C++
    UE4Editor-Win64-DebugGame.exe!FEngineLoop::Tick() Line 3967 C++
    [Inline Frame] UE4Editor-Win64-DebugGame.exe!EngineTick() Line 62   C++
    UE4Editor-Win64-DebugGame.exe!GuardedMain(const wchar_t * CmdLine, HINSTANCE__ * hInInstance, HINSTANCE__ * hPrevInstance, int nCmdShow) Line 168   C++
    UE4Editor-Win64-DebugGame.exe!WinMain(HINSTANCE__ * hInInstance, HINSTANCE__ * hPrevInstance, char * __formal, int nCmdShow) Line 261   C++

If I understand the callstack correctly the error occurs when the object is deleted. But why can that be the problem? Could it be that both Unreal and the NX third party lib try to manage the memory?

Has anyone an idea what could cause these crashed and even an idea how to solve it? I'm happy to give more information if necessary.

Greetings, LaRanaRica

I found that using the debug runtime library often can cause memory errors when using NXOpen. You can change this with the linker setting /MD (indstead of /MDd).

I know this is late, but I have a much better solution than to build in Release mode instead of Debug mode, because I've had this issue many times before and solved it using the same simple solution.

I don't know exactly why this occurs with NX Open C++, but it only happens when using std::vector (which some NX Open C++ APIs return) and Debug mode, together. You can fix it by adding this line to the top of your C++ source file, before all the #include s:

#define _ITERATOR_DEBUG_LEVEL 0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM