COM object that has been separated from its underlying RCW cannot be used

COM object that has been separated from its underlying RCW cannot be used

rui.frazao.oliveira
Contributor Contributor
6,092 Views
9 Replies
Message 1 of 10

COM object that has been separated from its underlying RCW cannot be used

rui.frazao.oliveira
Contributor
Contributor

I created an Addin that insert a custom Ribbon bar in Inventor.

 

When I run this Addin in Inventor 2015 or 2016 don’t give any error message.

 

But in Inventor 2017 or 2018, when I close the Inventor appears the message error “COM object that has been separated from its underlying RCW cannot be used”.

 

What could be?

 

Thanks & regards,

Rui

0 Likes
Accepted solutions (1)
6,093 Views
9 Replies
Replies (9)
Message 2 of 10

FRFR1426
Collaborator
Collaborator

You have:

 

GC.Collect();
GC.WaitForPendingFinalizers();

in your IExternalApplication.OnShutdown method?

 

For explanations, look at this post.

Maxence DELANNOY
Manager
Add-ins development for Autodesk software products
http://wiip.fr
0 Likes
Message 3 of 10

rui.frazao.oliveira
Contributor
Contributor

I have in Deactivate function the code:

 

                System.GC.Collect();
                System.GC.WaitForPendingFinalizers();

And the error happen with this code.

 

0 Likes
Message 4 of 10

FRFR1426
Collaborator
Collaborator

You also have to put at null all the fields of your class which implements ApplicationAddInServer else the COM object they contain will not be collected. And if there are static fields somewhere, you have to put them at null also.

 

And sorry I was talking of ApplicationAddInServer.Deactivate, IExternalApplication.OnShutdown is for Revit.

Maxence DELANNOY
Manager
Add-ins development for Autodesk software products
http://wiip.fr
0 Likes
Message 5 of 10

rui.frazao.oliveira
Contributor
Contributor

I just tried to clean everything in Deactivate function without success.

 

I have tried to insert an try/catch in Deactivate function to cature the error, but without success either.

 

        public void Deactivate()
        {
            try
            {

            ...

            }
            catch (Exception ex)
            {

            }
0 Likes
Message 6 of 10

rui.frazao.oliveira
Contributor
Contributor

There is some way to catch this error?

 

I am debugging code but the last step I catch have been the Deactivate() function and the error only appears after this.

 

Thanks & Regards

0 Likes
Message 7 of 10

FRFR1426
Collaborator
Collaborator

May be you can enable unmanaged debugging and add the Autodesk public symbols server in your project settings. There is instructions here for AutoCAD: http://through-the-interface.typepad.com/through_the_interface/2011/06/public-symbols-for-autocad-20... and here for 3ds Max: https://area.autodesk.com/blogs/the-3ds-max-blog/debug_symbol_server_for_3ds_max_2012/

 

Not sure there is symbols for Inventor, though.

Maxence DELANNOY
Manager
Add-ins development for Autodesk software products
http://wiip.fr
0 Likes
Message 8 of 10

rui.frazao.oliveira
Contributor
Contributor
Accepted solution

It is solved.

 

In Activate function I had something like

 

AddinGlobal.iApp = siteObj.Application;

Capture.JPG

 

And in Deactivate function I had the code:

 

if (AddinGlobal.iApp != null) Marshal.FinalReleaseComObject(AddinGlobal.iApp);
                AddinGlobal.iApp = null;

After comment these 2 lines of code in deactivate function, the error disappeared.

 

Thanks & regards,

Rui

 

0 Likes
Message 9 of 10

Anonymous
Not applicable

Hi how do you do this in very basic terms, step by step as i have no idea how to get to the code but i have this error

thanks, all the solutions are above my skill level thanks

0 Likes
Message 10 of 10

FRFR1426
Collaborator
Collaborator

Try to comment all calls to Marshal.ReleaseComObject() or Marshal.FinalReleaseComObject()

Maxence DELANNOY
Manager
Add-ins development for Autodesk software products
http://wiip.fr
0 Likes