UE 4/5 Crashes, Hello World in Third Party Library integration, EXCEPTION_ACCESS_VIOLATION

Hello All !

I’m fighting with a EXCEPTION_ACCESS_VIOLATION when trying to integrate an Hello World string from a Third Party C++ Library. The strange thing is that the error does not happen every time.

The first time I run the game, everything works (but not every time). The second time I usually have less luck and Unreal Engine crashes.

The first time, usually it works.
Capture ok

The second time, usually it crashes.

Here, you will find the UE log file:

[2022.06.14-09.42.33:104][773]LogMaterial: Error: MaterialInstance "M_HLOD0_OpenWorld_MainGrid_L1_X-1_Y0_DL0LandscapeStreamingProxy_D7T4VF4LBP34PHV9XOEVM5TIG_1_2_5_0" parameter 'NormalTexture' assigned texture "HLOD0_OpenWorld_MainGrid_L1_X-1_Y0_DL0T_LandscapeStreamingProxy_D7T4VF4LBP34PHV9XOEVM5TIG_1_2_5_0_Normal" requires non-virtual texture

[2022.06.14-09.42.33:104][773]LogStreaming: Display: FlushAsyncLoading: 48 QueuedPackages, 0 AsyncPackages

[2022.06.14-09.42.34:387][773]LogWindows: Error: === Critical error: ===

[2022.06.14-09.42.34:387][773]LogWindows: Error:

[2022.06.14-09.42.34:387][773]LogWindows: Error: Fatal error!

[2022.06.14-09.42.34:387][773]LogWindows: Error:

[2022.06.14-09.42.34:387][773]LogWindows: Error: Unhandled Exception: EXCEPTION_ACCESS_VIOLATION reading address 0x0000020820800070

[2022.06.14-09.42.34:387][773]LogWindows: Error:

[2022.06.14-09.42.34:387][773]LogWindows: Error: [Callstack] 0x00007ffb02de2f4b UnrealEditor-Core.dll!UnknownFunction []

[2022.06.14-09.42.34:387][773]LogWindows: Error: [Callstack] 0x00007ffb0282a57c UnrealEditor-Core.dll!UnknownFunction []

[2022.06.14-09.42.34:387][773]LogWindows: Error: [Callstack] 0x00007ffac16a1ea1 UnrealEditor-LibBonjour.dll!ABonjourActor::BeginPlay() [C:\Users\moi\Documents\Developpements\UE5\TestExternalLibrary\Plugins\LibBonjour\Source\LibBonjour\Private\BonjourActor.cpp:20]

[2022.06.14-09.42.34:387][773]LogWindows: Error: [Callstack] 0x00007ffb03d14e3f UnrealEditor-Engine.dll!UnknownFunction []

[2022.06.14-09.42.34:387][773]LogWindows: Error: [Callstack] 0x00007ffb05b0ec92 UnrealEditor-Engine.dll!UnknownFunction []

[2022.06.14-09.42.34:387][773]LogWindows: Error: [Callstack] 0x00007ffb04649e21 UnrealEditor-Engine.dll!UnknownFunction []

[2022.06.14-09.42.34:387][773]LogWindows: Error: [Callstack] 0x00007ffb05a38238 UnrealEditor-Engine.dll!UnknownFunction []

[2022.06.14-09.42.34:387][773]LogWindows: Error: [Callstack] 0x00007ffb04604725 UnrealEditor-Engine.dll!UnknownFunction []

[2022.06.14-09.42.34:387][773]LogWindows: Error: [Callstack] 0x00007ffb00b49898 UnrealEditor-UnrealEd.dll!UnknownFunction []

[2022.06.14-09.42.34:387][773]LogWindows: Error: [Callstack] 0x00007ffb00b7ec66 UnrealEditor-UnrealEd.dll!UnknownFunction []

[2022.06.14-09.42.34:387][773]LogWindows: Error: [Callstack] 0x00007ffb00b4a342 UnrealEditor-UnrealEd.dll!UnknownFunction []

[2022.06.14-09.42.34:387][773]LogWindows: Error: [Callstack] 0x00007ffb00b9d42a UnrealEditor-UnrealEd.dll!UnknownFunction []

[2022.06.14-09.42.34:387][773]LogWindows: Error: [Callstack] 0x00007ffb00ba0c37 UnrealEditor-UnrealEd.dll!UnknownFunction []

[2022.06.14-09.42.34:387][773]LogWindows: Error: [Callstack] 0x00007ffb00ba0521 UnrealEditor-UnrealEd.dll!UnknownFunction []

[2022.06.14-09.42.34:387][773]LogWindows: Error: [Callstack] 0x00007ffb0053dcb3 UnrealEditor-UnrealEd.dll!UnknownFunction []

[2022.06.14-09.42.34:387][773]LogWindows: Error: [Callstack] 0x00007ffb00eca666 UnrealEditor-UnrealEd.dll!UnknownFunction []

[2022.06.14-09.42.34:387][773]LogWindows: Error: [Callstack] 0x00007ff6d0a282f6 UnrealEditor.exe!UnknownFunction []

[2022.06.14-09.42.34:387][773]LogWindows: Error: [Callstack] 0x00007ff6d0a40d9c UnrealEditor.exe!UnknownFunction []

[2022.06.14-09.42.34:387][773]LogWindows: Error: [Callstack] 0x00007ff6d0a40e8a UnrealEditor.exe!UnknownFunction []

[2022.06.14-09.42.34:387][773]LogWindows: Error: [Callstack] 0x00007ff6d0a43c4d UnrealEditor.exe!UnknownFunction []

[2022.06.14-09.42.34:387][773]LogWindows: Error: [Callstack] 0x00007ff6d0a55564 UnrealEditor.exe!UnknownFunction []

[2022.06.14-09.42.34:387][773]LogWindows: Error: [Callstack] 0x00007ff6d0a58736 UnrealEditor.exe!UnknownFunction []

[2022.06.14-09.42.34:387][773]LogWindows: Error: [Callstack] 0x00007ffbb8397034 KERNEL32.DLL!UnknownFunction []

[2022.06.14-09.42.34:387][773]LogWindows: Error: [Callstack] 0x00007ffbb9b22651 ntdll.dll!UnknownFunction []

[2022.06.14-09.42.34:387][773]LogWindows: Error:

[2022.06.14-09.42.34:399][773]LogExit: Executing StaticShutdownAfterError

[2022.06.14-09.42.34:400][773]LogWindows: FPlatformMisc::RequestExit(1)

[2022.06.14-09.42.34:400][773]LogWindows: FPlatformMisc::RequestExitWithStatus(1, 3)

[2022.06.14-09.42.34:400][773]LogCore: Engine exit requested (reason: Win RequestExit)

[2022.06.14-09.42.34:406][773]Log file closed, 06/14/22 11:42:34

My Project is a blank C++ project.

I created a blank Third Party Plugin (from the Plugin menu) and added a SayHello() function in the ExampleLibrary files, like this:

// ExampleLibrary.h
#if defined _WIN32 || defined _WIN64
#define EXAMPLELIBRARY_IMPORT __declspec(dllimport)
#elif defined __linux__
#define EXAMPLELIBRARY_IMPORT __attribute__((visibility("default")))
#else
#define EXAMPLELIBRARY_IMPORT
#endif
#include <string>

EXAMPLELIBRARY_IMPORT void ExampleLibraryFunction();
EXAMPLELIBRARY_IMPORT std::string SayHelloFromLib();
// ExampleLibrary.cpp
#if defined _WIN32 || defined _WIN64
    #include <Windows.h>

    #define EXAMPLELIBRARY_EXPORT __declspec(dllexport)
#else
    #include <stdio.h>
#endif

#ifndef EXAMPLELIBRARY_EXPORT
    #define EXAMPLELIBRARY_EXPORT
#endif
#include <string>

EXAMPLELIBRARY_EXPORT void ExampleLibraryFunction()
{
#if defined _WIN32 || defined _WIN64
	MessageBox(NULL, TEXT("ExampleLibrary.dll chargée depuis le Plugin Third Party."), TEXT("Third Party Plugin"), MB_OK);
#else
    printf("ExampleLibrary.dll chargée depuis le Plugin Third Party.");
#endif
}

EXAMPLELIBRARY_EXPORT std::string SayHelloFromLib() {
    return "Bonjour depuis la librairie externe !";
}

The SayHello function just returns a std:string.

I created an Actor in my plugin with UE and I edited the BeginPlay function:

// BonjourActor.cpp
void ABonjourActor::BeginPlay()
{
	Super::BeginPlay();

	FString Message = FString(UTF8_TO_TCHAR(SayHelloFromLib().c_str()));	
	GEngine->AddOnScreenDebugMessage(-1, 10.0, FColor::White, Message);
}

Here, you will find my *.Build.cs files:

// TestExternalLibrary.Build.cs (the Build.cs of the UE Project)
public class TestExternalLibrary : ModuleRules
{
	public TestExternalLibrary(ReadOnlyTargetRules Target) : base(Target)
	{
		PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs;
	
		PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "LibBonjour" });

		PrivateDependencyModuleNames.AddRange(new string[] { });
	}
}
// LibBonjour.Build.cs (the Build.cs of the UE Plugin)
PublicDependencyModuleNames.AddRange(
			new string[]
			{
				"Core",
				"LibBonjourLibrary",
				"Projects",
				"Engine",
				"CoreUObject"
				// ... add other public dependencies that you statically link with here ...
			}
			);
// LibBonjourLibrary.Build.cs (the Build.cs of the third party library)
if (Target.Platform == UnrealTargetPlatform.Win64)
		{
			// Add the import library
			PublicAdditionalLibraries.Add(Path.Combine(ModuleDirectory, "x64", "Release", "ExampleLibrary.lib"));

			// Delay-load the DLL, so we can load it from the right place first
			PublicDelayLoadDLLs.Add("ExampleLibrary.dll");

			// Ensure that the DLL is staged along with the executable
			RuntimeDependencies.Add("$(PluginDir)/Binaries/ThirdParty/LibBonjourLibrary/Win64/ExampleLibrary.dll");
        }
        else if (Target.Platform == UnrealTargetPlatform.Mac)
        {
            PublicDelayLoadDLLs.Add(Path.Combine(ModuleDirectory, "Mac", "Release", "libExampleLibrary.dylib"));
            RuntimeDependencies.Add("$(PluginDir)/Source/ThirdParty/LibBonjourLibrary/Mac/Release/libExampleLibrary.dylib");
        }
        else if (Target.Platform == UnrealTargetPlatform.Linux)
		{
			string ExampleSoPath = Path.Combine("$(PluginDir)", "Binaries", "ThirdParty", "LibBonjourLibrary", "Linux", "x86_64-unknown-linux-gnu", "libExampleLibrary.so");
			PublicAdditionalLibraries.Add(ExampleSoPath);
			PublicDelayLoadDLLs.Add(ExampleSoPath);
			RuntimeDependencies.Add(ExampleSoPath);
		}

I will be delighted if anyone could help me with this problem. I’m looking for a solution since few days and I really need some support.

Thank you in advance.

Regards.

maybe that library is just faulty, has some errors inside and doesn’t work properly

Hello dorusoftware,

Thanks for your help.
The library I use is just a function which returns an std::string. You can see the code of this library above (ExampleLibrary.h/.cpp).

...
EXAMPLELIBRARY_EXPORT std::string SayHelloFromLib() {
    return "Bonjour depuis la librairie externe !";
}
...

I have recompiled this library many times. It comes from the Unreal Engine template provided. I have just added the SayHelloFromLib function.

I don’t know much about programming so not the best user to help you.

One hint I could give that maybe is related is to use the engine implementation of the data types.

so instead of using the c++ data types like string use FString

Thanks for the link. But, the goal is to link with an external library which is written in standard c++ (it’s the feature of the third party library template provided by Epic Games).

I’ve found the solution.

It’s due to a non standard stl c++ library implementation ( Why doesn’t UE utilize STL containers? - Programming & Scripting / C++ - Unreal Engine Forums).
So, I must not use std::string in my DLL interface (ExampleLibrary.h) and must replace it by:

EXAMPLELIBRARY_IMPORT char * SayHelloFromLib();