what's the best practice to do the garbage collection?

G

Guest

hi,

I am writing a COM Addin for outlook using .Net Framwork 2.0, which
generates the managed code. However, the host application Outlook 2003
belongs to the unmanaged world. In the CLR environment, the .Net Framework
will do the garbage collection automatically but in an undeteministic way,
that means I don't have any idea of exactly when the memory will be released.
Therefore it might have some potential problems if I don't write explicitly
the garbage collection code in the Com Add in. For instance, if I close the
outlook 2003, which has already load the COM add-in, and then quickly reopen
that outlook 2003. The system will pop out a messgge says like "operation
failed". I guess that's because I don't do the garbage collection, is that
correct?

To solve this issue, I try to raise the quit event handler for the
outlook.application object. In that handler, it basically does the following

outlookApp = nothing
GC.Collect()
GC.WaitForPendingFinalizers()
GC.Collect()
GC.WaitForPendingFinalizers()

However, the problem is not solved successfully by using that way. I am
doubting if I should put such code in somewhere else?

Any suggestion would be greatly appreciated.
 
K

Ken Slovak - [MVP - Outlook]

The Quit event is pretty useless. What is needed is an event handler for
Explorer.Close and one for Inspector.Close. In each you would test for the
closing of the last Explorer and/or Inspector in their respective
collections. That's where you should explicitly release all your objects
using MarshallReleaseComObject and then after all that explicitly calling
the garbage collector.
 
G

Guest

thanks so much
--
John 3:16


Ken Slovak - said:
The Quit event is pretty useless. What is needed is an event handler for
Explorer.Close and one for Inspector.Close. In each you would test for the
closing of the last Explorer and/or Inspector in their respective
collections. That's where you should explicitly release all your objects
using MarshallReleaseComObject and then after all that explicitly calling
the garbage collector.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top