I would encourage everyone to completely avoid use of the MEL command "unloadPlugin –force pluginName" or its Python equivalent cmds.unloadPlugin("pluginName", force=true). And, especially when writing vital tests.
The force flag of the unloadPlugin is a documented (and almost guaranteed way) of creating dangling pointers. Here's what Maya's User Manual has to say about it:
Unload the plug-in even if it is providing services. This is not recommended. If you unload a plug-in that implements a node or data type in the scene, those instances will be converted to unknown nodes or data and the scene will no longer behave properly. Maya may become unstable or even crash. If you use this flag you are advised to save your scene in MayaAscii format and restart Maya as soon as possible.
A plug-in which has a node in the DG (or a command in the undo queue) cannot be be safely unloaded. Forcing the unload will only create dangling pointers, memory corruption and might lead to crashes. The normal solution is simply to empty the DG and the undo queue by performing a "file –new". Normally, you should be able to unload the plug-in afterward.
Unfortunately, there are some issues with MFnPlugin::registerModelEditorCommand(). Any plug-in registering a model editor command would immediately be marked as being unsafe to unload forever. Emptying the scene and the undo queue would not help. Getting of all UI elements created by the invocation on the registered model editor command would not help neither. I.e. There would be absolutely no way to safely unload the plug-in without creating dangling pointers, memory corruptions and potential crashes…
Forcing the plug-in to unload while it still has nodes present in the scene still isn't harmless. There's information about the node which Maya will need to make or break connections, set or get attribute values, delete the node, etc, and some of that information is lost when the plug-in unloads. Maya won't know that the data is no longer there so it can end up chasing bogus pointers with all the instability that brings.
Alternatively if you want to store some settings in a scene without using a plug-in node, you can do one of the following:
- Store them in the scene's fileInfo database. See the 'fileInfo' command for more details.
- Create an instance of a low-overhead built-in node, like 'choose', and then add dynamic attrs to it to store your settings.
- Same as the previous suggestion except that as of Maya 2013.5 you can also store the settings as metadata on the node.