When freeing up COM objects in your Inventor add-in, don’t use FinalReleaseComObject() as it will badly affect other add-ins. It fully releases the object, so that other add-ins previously referencing it will not be able to access it either.
Here is an example.
AddIn1 does this when being unloaded:
public void Deactivate()
{
Marshal.FinalReleaseComObject(m_inventorApplication);
m_inventorApplication = null;
GC.Collect();
GC.WaitForPendingFinalizers();
}
AddIn2 has the following code executing when its button is clicked:
private void M_buttonDefinition_OnExecute(NameValueMap Context)
{
try
{
MessageBox.Show(m_inventorApplication.ActiveDocument.DisplayName, "m_inventorApplication.ActiveDocument.DisplayName");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Accessing m_inventorApplication");
}
}
If I unload AddIn1 and try to use AddIn2’s command, I’ll run into this:
(error message says: “COM object that has been separated from its underlying RCW cannot be used.”)
Instead of using FinalReleaseComObject(), you could use ReleaseComObject() or even just setting the variable to null might be enough.
You can still call GC.Collect() & GC.WaitForPendingFinalizers() to trigger the release of the objects.
-Adam


Leave a Reply