HowTo:Handle Initialization and Termination of Native Objects in Plug-ins?
From DocR21
Every plug-in contains a factory class that needs to derive from the IPluginFactory interface, declared in RadfCore, in order to be recognized and used by RADF.
The IPluginFactory's Initialize() and Terminate() methods must be the very first and the very last methods to call for any plug-in.
The RADF application lifetime is as follows:
- Start RADF.
- Start the Modeler.
- Load and Initialize plug-in factories.
- ....
- Plug-ins are used.
- ....
- Terminate plug-in factories.
- Stop the Modeler.
- Unload RADF.
If some initialization and/or termination code needs to be called exactly once per RADF application lifetime, these methods are the best place in which to add such code. They are particularly helpful when dealing with wrappers over native code, for instance to avoid memory leaks and crashes. Native objects may need an explicit initialization and termination as well.
These methods when implemented must contain such plug-in-specific logic, so that after executing Initialize(), the plug-in's functionality may be used, and after executing Terminate(), the plug-in cleanup is completed.
The Initialize() method is guaranteed to be called during application startup, immediately after all plug-ins are loaded.
The Terminate() method is guaranteed to be called just before the RADF main window is closed.
If native objects need to be initialized, consider doing so in the Initialize() method of their plug-in factory.
If native objects need to be disposed, consider doing so in the Terminate() method of their plug-in factory. Relying on object destructors (finalizers) to do the final cleanup may lead to a situation of calling the destructor after the StopModeller call has already ended. That may lead to an "Access Violation" and other undesirable results, because the destructor tries to cleanup resources already disposed by the StopModeller call.
Examples
Examples of these functions' implementations is found in factory classes for HoopsViewer and Defeature.
