Third party c++ library class with virtual destructor : crash on delete

Hello,

I’m currently working on a third party c++ library, that I use in a c++ unreal editor plugin.

It’s integrated as a library, the plugin linking with it :

PublicAdditionalLibraries.AddRange(
	new string[]
	{
        	Path.Combine(libPath, "MyLib.lib")
	}
);

PublicRuntimeLibraryPaths.AddRange(
    new string[]
    {
        Path.Combine(LibPath, "MyLib.dll")
    }
);

I have a crash problem when allocating/deleting a library class instance in the plugin. It crashes as soon as there is a constructor defined in cpp (in the library assembly, won’t crash if inlined in the .h), and there is a virtual destructor.

Here the minimum code to reproduce it :
In the library .h :

class Test
{
public:
	virtual ~Test() = default;
	Test();
};

In the library .cpp :

Test::Test()
{
}

In the unreal plugin :

Test * test = new Test();
delete test; // <<  __debugbreak() happens here

It seems to be a heap corruption.

Can it be an incompatibility between the binary version of unreal engine 5.4.1 (I don’t built it myself), and the last visual studio 2022 I use to build my projects? What should I do to get this working ?

I found the problem, I use cmake to m!anage my project, and exported my classes with :

set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS true)

This seems not to export everything needed (the virtual table for example), instead I add to manually decorate my classes with the classic MYLIB_EXPORT (using __declspec).

cmake can generate a file containing this define with the generate_export_header command (in the GenerateExportHeader module).

1 Like