Cleaning-up Memory

To ensure the plug-in framework unloads the plug-in DLLs when they are no longer required and releases associated memory, the clients should follow an explicit memory cleanup procedure. The main purpose is to release memory and close handles held on unused plug-in implementations and their associated DLLs. The memory clean-up should be performed after all instantiated interface objects have been deleted.

The plug-in framework client-side library implements a deferred cleanup mechanism in the client process to handle destroyed plug-ins. Following a call to REComSession::DestroyedImplementation() in the destructor, the plug-in’s reference count is decremented to zero and the client-side library has a “last destroyed last plug-in” garbage pointer. The internal framework memory associated with the garbage plug-in is released. The handles on the DLL are closed at the beginning of the next call to REComSession::DestroyedImplementation(). This chain of deferred cleanup in the client thread requires the final plug-in in the garbage queue to be released. Hence there is a requirement for explicit memory clean-up. The memory clean-up is performed by the clients of the plug-in framework.
Note: The destructor of the interface implementation deletes the instantiated objects obtained from an interface when they are no longer required by calling the static function REComSession::DestroyedImplementation().
inline CExampleInterfaceDefinition::~CExampleInterfaceDefinition()
    {
    REComSession::DestroyedImplementation(iDtor_ID_Key);
    }

  1. Call REComSession::FinalClose() before shut down to ensure the plug-in framework executes the final cleanup steps. Note: REComSession::FinalClose() should not be called from a plug-in implementation class destructor, especially following a REComSession::DestroyedImplementation(). There is a risk that the plug-in's supporting DLL may immediately be unloaded by the Kernel due to the closure of RLibrary handles. It can result in a KERN-EXEC if the destructor call stack is still inside the DLL that is unloaded.

  2. If the plug-in framework is used in a library, then REComSession::FinalClose() should be invoked within the library's shutdown function. This makes the library's use of plug-in framework transparent to the clients. It is not an error if both the library and client call REComSession::FinalClose().