Skip to content

Commit

Permalink
#5361: Module instance references automatically clear their cache onc…
Browse files Browse the repository at this point in the history
…e all modules have been uninitialised.

Since the core library is not unloaded from memory after calling dlclose() in Linux, no statics re-initialisation happens, so the references need to be safely cleared to allow the unit test sequence to run successfully.
  • Loading branch information
codereader committed Oct 19, 2020
1 parent 05fac1c commit 3256449
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
26 changes: 13 additions & 13 deletions include/imodule.h
Expand Up @@ -401,38 +401,38 @@ namespace module
private:
const char* const _moduleName;
ModuleType* _instancePtr;
IModuleRegistry::InstanceId _registryInstanceId;

public:
InstanceReference(const char* const moduleName) :
_moduleName(moduleName),
_instancePtr(nullptr),
_registryInstanceId(0)
_instancePtr(nullptr)
{
acquireReference();
}

// Cast-operator used to access the module reference
inline operator ModuleType&()
{
#ifdef MODULE_REFERENCES_SUPPORT_INVALIDATION
// Check if we have an instance or if it is outdated
if (_instancePtr == nullptr ||
_registryInstanceId != GlobalModuleRegistry().getInstanceId())
{
if (_instancePtr == nullptr)
{
acquireReference();
}
#endif

return *_instancePtr;
}

private:
void acquireReference()
{
_instancePtr = std::dynamic_pointer_cast<ModuleType>(
GlobalModuleRegistry().getModule(_moduleName)).get();
// Save the instance ID of the registry - if this ever changes
// the above reference is treated as invalid
_registryInstanceId = GlobalModuleRegistry().getInstanceId();
auto& registry = GlobalModuleRegistry();

_instancePtr = std::dynamic_pointer_cast<ModuleType>(registry.getModule(_moduleName)).get();

registry.signal_allModulesUninitialised().connect([this]
{
_instancePtr = nullptr;
});
}
};

Expand Down
2 changes: 1 addition & 1 deletion tools/msvc/properties/Tests.props
Expand Up @@ -10,7 +10,7 @@
<AdditionalDependencies>scenelib.lib;mathlib.lib;xmlutillib.lib;modulelib.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
<ClCompile>
<PreprocessorDefinitions>MODULE_REFERENCES_SUPPORT_INVALIDATION;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
</ItemDefinitionGroup>
<ItemGroup />
Expand Down

0 comments on commit 3256449

Please sign in to comment.