views:

260

answers:

2

Hello,

I have created a tool that imports an excel sheet. The excel COM object is created during the lifetime of the app. I have applied the MVP pattern to my tool so that VIEW and Presenter are seperating the UI and logic.

The vIEW that is a WinForm is having a Dispose() method due inheritance from From class, which is overriden in the Deisgner code.

protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
        _presenter.Dispose();
    }

Thats literally where I insert my dispose call o my presenter, which itself is a managed code, but yet contains unmanaged COM objects.

The presenter is then trying to release the COM object by doing this:

public void Dispose()
    {
        Int32 countDown = 1; 
        while (countDown > 0)
            countDown = Marshal.ReleaseComObject(_excelObj);
    }

If I open the app without importing anything, the Excel process disappears after closing teh app. Otehrwise if the app is used to import a sheet, the excel object doesn't let go anymore and remains in memory., even afte rthe job is sucesfully executed or cancelled.

I have found a good link that helped me further, but the problem still remains: http://stackoverflow.com/questions/814821/how-do-i-ensure-that-objects-are-disposed-of-properly-in-net

Thanks for help,

A: 

I've had the same issues before when working with Excel COM objects. What you will have to do in your application is in your Dispose method, specifically check to see if the Excel process is running and kill it manually.

Just make sure that any changes you've made have been saved first.

Daven Patel
The danger here is, what if another Excel sheet unrelated to mine is already open? Then i would kill the wrong process by mistake and loose data.
Kave
+2  A: 

Try using FinalReleaseComObject instead of ReleaseComObject. Note that FinalReleaseComObject will not need to be used in a loop the way ReleaseComObject does.

For a more in-depth explanation, take a look at this reply to another question since there are likely other Excel objects you can be clearing up to ensure references aren't held. That entire question is helpful but the linked post in particular should sum things up.

Ahmad Mageed