Access violation crashes when using third party library code

Hey all,

Some background information first.

I’m currently trying to write a c++ class that can parse a file and generate a tree structure from it that I will be able to use for Inverse Kinematics. To do this I’m making use of some ROS libraries that are able to incorporate the functionality that I need, namely urdf and kdl.

I’ve managed to link the libraries into the build system through following the instructions here A new, community-hosted Unreal Engine Wiki - Announcements - Epic Developer Community Forums

My class can now recognize the structures that I want to make use of, and compiles successfully, but unfortunately Unreal crashes when I try to stop it running,

The code below is from my c++ file, and the issue seems to revolve around garbage collection of my urdf model, as it manages to successfully parse the file only to crash on stopping.

   #include "AIKTester.h"
    #include "Runtime/Core/Public/Misc/Paths.h"
    #include "Runtime/Core/Public/HAL/FileManager.h"
    #include <string>
    
    // Sets default values
    AAIKTester::AAIKTester()
    {
     	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
    	PrimaryActorTick.bCanEverTick = true;
    
    }
    
    // Called when the game starts or when spawned
    void AAIKTester::BeginPlay()
    {
    	Super::BeginPlay();
    	urdfParser();
    }
    
    // Called every frame
    void AAIKTester::Tick(float DeltaTime)
    {
    	Super::Tick(DeltaTime);
    
    }
    
    bool AAIKTester::urdfParser()
    {
    	// Grab relative project path for file system and convert to string
    	FString RelativeProjPath = FPaths::ProjectDir();
    	std::string ProjPath(TCHAR_TO_UTF8(*RelativeProjPath));
    
    	// Try parse urdf file into model using relative project path and urdf file location
    	if (!my_model.initFile(ProjPath + "Source/Project/robot.urdf")) {
    		UE_LOG(LogTemp, Log, TEXT("Failed to parse urdf file"));
    		return false;
    	}
    	else {
    		UE_LOG(LogTemp, Log, TEXT("Parsed the urdf file"));
    		return true;
    	}
    }

And here is the included header file:

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "../../ThirdParty/ROS2/Includes/urdf/model.h"
#include "../../ThirdParty/ROS2/Includes/kdl/tree.hpp"
#include "../../ThirdParty/ROS2/Includes/kdl_parser/kdl_parser.hpp"
#include "AIKTester.generated.h"


UCLASS()
class PROJECT_API AAIKTester : public AActor
{
	GENERATED_BODY()
	
public:	
	// Sets default values for this actor's properties
	AAIKTester();

protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;

public:	
	// Called every frame
	virtual void Tick(float DeltaTime) override;

	urdf::Model my_model;
	bool urdfParser();
};

I’ve tried to get my head around the call stack, but unfortunately I can’t get any clarity about how to solve this problem. The error code and call stack seem to change every other time running it, most recently displaying:

Exception thrown at 0x00007FF80FEE731E (nvwgf2umx.dll) in UE4Editor.exe: 0xC0000005: Access violation reading location 0xFFFFFFFFFFFFFFFF.

Any help would be greatly appreciated, thanks.

Hello bradmatt,

I am having a similar issue with Unreal crashing when a 3rd party DLL is incorporated. Were you able to resolve it? I am stumped.

Hi,

Unfortunately not. The issue in my case seems to be linked to how Unreal handles the garbage collection of UClasses, and it not being able to delete the objects created through the 3rd party library.

I’ve not spent any more time looking at the issue as of late, so haven’t made any progress past that point yet. I may have another look if time permits.

Hi,
I managed to “discover” a resolution to my issue - might be useful to you as well. Apparently, third party libraries (DLLs) can only work if they are compiled in release configuration - i.e. no debug info present. I was compiling my DLL in debug mode. With one line change in CMakeLists.txt “set(CMAKE_BUILD_TYPE Release)”, I was able to resolve all my issues! This site helped: Unreal Engine: Including a Third-Party Library (on the example of the Point Cloud Library and Boost) [Tutorial] [Download] – Valentin Kraft's Portfolio