user38230
user38230

Reputation: 665

Dispose C# DLL marked for COM interop

I created a vanilla C# dll and marked it for COM inter op. The dll is a plain user form with no functionality as this is an experiment.

I registered the DLL and opened active x test container and instantiated my COM object. It shows up in the test container and i can view the exposed methods of the control - these are the default methods and not created by me.

i then exit from the active x test container and i noticed that test container was still lurking around in task manager and that i had to kill the process manually. This leads me to believe that the test container still holds a reference to my C# dll exposed for com inter op.

The default dispose method in the designer.cs is

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


   }

When i used the test container with Quick time, it invoked quick time as expected and when i closed the test container it disposed correctly and did not leave a footprint in the task manager.

The question - is there a specific thing that i must do in my dispose method? Also, this may not be relevant, but when i create a test project and it launches my c# control and i click on the close button, it closes my test form but the test application ins still running in debug mode - the run button is disabled.

Upvotes: 3

Views: 3532

Answers (3)

HasaniH
HasaniH

Reputation: 8402

In addition to overriding Dispose(bool) you need to override IDisposable.Dispose() in that method you call Dispose(true) then call GC.SurpressFinalize(this). Also you have to call Dispose() on all members that implement IDisposable in the override of Dispose(bool)

Upvotes: 0

JoshBerke
JoshBerke

Reputation: 67108

You could try:

public void IDisposable.Dispose()
{     
   System.Runtime.InteropServices.Marshal.ReleaseComObject(this);
}

Typically you do this when consuming COM objects from within .net, not sure about the other way. But it can't hurt to try. Let me know how it worked out;-)

Edit:

You can also try and supress finalization:

GC.SuppressFinalize(this);

Upvotes: 3

David Schmitt
David Schmitt

Reputation: 59355

The MSDN has a quite comprehensive article on how to implement IDisposable.

Upvotes: 1

Related Questions