Is it possible to use FThreadSingleton outside of Core module on Windows non-monolithic builds?

If you attempt to inherit a class from FThreadSingleton and that class is not in the Core module, you will get a compile error on Windows non-monolithic builds.

The reason is because FThreadSingleton is marked CORE_API, and VC++ expects any of those instantiations of FThreadSingleton::Get to be exported from Core.

Removing the CORE_API from the FThreadSingleton declaration of course causes compile errors with FMemoryStack and other classes which use it in Core, which is why I imagine it was added in the first place.

In the general case the solution to this is to explicitly export specific template instantiations from the DLL. This requires another macro for each module, CORE_TEMPLATE_EXTERN.

when compiling the module:


when compiling other modules


then to export the template instantiation:

CORE_TEMPLATE_EXTERN template class CORE_API Foo<Bob>;

Unfortunately, in this specific case, I’m not sure if this will work with the way FThreadSingleton is designed, because you get a chicken and egg scenario because the derived class is not fully defined and the template uses the derived class.

You could maybe try this, but I doubt it would work:

class FFoo;

CORE_TEMPLATE_EXTERN template class CORE_API FThreadSingleton<FFoo>;
class CORE_API FFoo : public FThreadSingleton<FFoo>

Not sure exactly how to work around this to be honest. For now I’m just duplicating the FThreadSingleton functionality in my class but it’d be nice to be able to use this helper in other modules.

Here’s a potential way to avoid the chicken and egg situation:

class FMemStackSingleton
// implementation of FMemStack

CORE_TEMPLATE_EXTERN template class CORE_API FThreadSingleton<FMemStackSingleton>;

typedef FThreadSingleton<FMemStackSingleton> FMemStack;

This would avoid the chicken-and-egg explicit instantiation problems, allow removing CORE_API from the declaration of FThreadSingleton, and the client code is still just FMemStack::Get()


Thanks for reporting this. We’ve added this issue to our backlog.