2

I have written a C# application that makes use of the Visio interop. I'm experiencing an event-related issue that is baffling me. Steps to reproduce:

  1. Launch Visio
  2. Launch my application. Visio instance is retrived, the object model can be used, and events are received and handled in my app.
  3. Close my app- leave Visio open.
  4. Launch my app. Visio instance is retrieved and the object model can be used, but events are NOT received by my app.

When my app opens, it retrieves an instance to the open Visio App using:

visioApp = (Microsoft.Office.Interop.Visio.Application)Marshal.GetActiveObject("Visio.Application");

I register events like so:

visioApp.DocumentOpened += new EApplication_DocumentOpenedEventHandler(onDocOpened);

When my app shuts down, i unregister the event like so:

visioApp.DocumentOpened -= onDocOpened;
visioApp = null;

It seems like I'm not properly unregistering / letting the instance go when my app shuts down, but thats just a guess. When my app starts the second time, i get no errors- it just doesn't respond to events. I haven't been able to find any further related info online (probably just not searching for the correct terms). Any help is greatly appreciated! I'll provide any more info that might be useful.

@Oscars answer was correct:

 Marshal.FinalReleaseComObject(_ap);

However I had one additional problem. Marshal.finalReleaseComObject was being called in a class destructor, which was apparently being called too late in the application shutdown sequence (even if I forced garbage collection), and was not properly releasing the reference. I now call this code earlier (in MainForm_Closing event) and it works well. Thanks for the help!

1 Answers1

0

Any exception? Are you correctly disposing the COM object with:

Marshal.ReleaseComObject(sheet);

How do I properly clean up Excel interop objects?

http://msdn.microsoft.com/es-es/library/system.runtime.interopservices.marshal.releasecomobject.aspx

Community
  • 1
  • 1
Oscar
  • 13,594
  • 8
  • 47
  • 75
  • I think this is highly likely th issue – Paul Sullivan Jul 16 '13 at 21:31
  • @Oscar I had actually tried Marshal.ReleaseComObject but it didnt seem to work in my application. However, I built a test app that simply retrieved a reference to visio, responded to an event, and release the visio reference as you mention. That works flawlessly. I'll have to do some more digging through my code to make sure I've properly released all objects obtained through the interop (documents, sheets, shapes, etc). Thanks for your help! At least now i'm on the right track! – Controlsfreek Jul 17 '13 at 14:06
  • Remember the 2 dots rule. Good luck! – Oscar Jul 17 '13 at 15:16
  • In my test application, I could only cause the problem to occur if i failed to unregister the events. I could comment out visioApp = null; and Marshal.ReleaseComObject(visioApp); and the problem would not occur. Interesting. I wish an exception were thrown to give some indication of the problem! – Controlsfreek Jul 17 '13 at 15:29
  • The 2 dots rule! I was not aware that COM objects were unmanaged! This will save my life! Now to go clean up my code.... THANK YOU. – Controlsfreek Jul 17 '13 at 15:33