Hi,
I’m trying to use a third party library in UE4 but I’m getting garbage data and crashes. I have determined that the cause of the problem is that the size and alignment requirement of std::function
differ between code compiled stand-alone and code compiled with the Unreal Build Tool as part of a plugin. In particular, this line produces different output:
std::cout << sizeof(std::function<void(int*)>) << ' ' << std::alignment_of< std::function<void(int*)> >() << '\n';
My stand-alone program prints 32 8
while adding this line to an Unreal Engine plugin prints 48 16
. It doesn’t matter if I use g++
or clang++
from the system (7.4.0 and 6.0.0, respectively), or if I use the clang++
provided by Unreal Engine in UnrealEngine/Engine/Extras/ThirdPartyNotUE/SDKs/HostLinux/Linux_x64/v13_clang-7.0.1-centos7/x86_64-unknown-linux-gnu/bin/clang++
(7.0.1) for the stand-alone test.
The effect of this is that a class such as the following from the third party library:
template<size_t NUM_BUFFERS>
class CustomContainer
{
CustomContainer();
void set(size_t buffer, size_t index, int value)
{
m_buffers[buffer][index] = value;
m_buffer_updated_callback(m_buffers[buffer]);
}
private:
std::function<void(int*)> m_buffer_updated_callback;
int* m_buffers[NUM_BUFFERS];
};
will access an incorrect element of m_buffers
, and possibly even read outside the array entirely, when set
is called from the plugin since the constructor will place the m_buffers
array at one offset while the inlined set
method, which contains code compiled by the Unreal Build Tool, will try to access the array at another offset.
What can I do to make the Unreal Build Tool create std::function
instances that are compatible with the normal/default std::function
?
Are there other classes where I can expect the same problem to occur?