2

I have a COM object that we are calling from C#. This works great, and I have my own pool of objects that I can use whenever I want. Now I need to kill the object. I've tried releasing the COM object explicitly and then garbage collecting from another thread, but that does nothing. Does anyone have any other ideas to kill this object? Thanks for the help.

I've tried

System.Runtime.InteropServices.Marshal.ReleaseComObject(myApp); 
GC.Collect(); 
GC.WaitForPendingFinalizers ();
myApp = null;

and I create it by

myApplication.ApplicationClass myApp = new myApplication.ApplicationClass();

this is the full code this com object is written in vb6, below is the C# code calling vb6 component

      myApplication.ApplicationClass myApp = new myApplication.ApplicationClass();

        string user = this._User;
        string pass = this._Pass;
        string company = this.companyNumber;

        try
        {
            if (myApp.Login(ref user, ref pass, ref company))

{ //Perform some action } else { throw new System.Exception(MESSAGE_LOGINERROR); } } Finally { System.Runtime.InteropServices.Marshal.ReleaseComObject(myApp);

            GC.Collect();
            GC.WaitForPendingFinalizers();
            M2MApp = null;

}

Thanks, Pradeep

4
  • What library ApplicationClass is from? Is it a microsoft excel/word instance wrapped underneath? Commented Jun 15, 2010 at 6:21
  • it is not a excel or word component it is a active x dll created by me. Commented Jun 15, 2010 at 6:26
  • Post the VB6 code that accesses VFP database. It could be your VB6 code that locks up the DB & not the problem from .net end. Commented Jun 16, 2010 at 12:21
  • why are you sending ref-pointers into your myApp.Login() - function ? Can that be your backward-reference ? Commented Jul 8, 2010 at 19:06

4 Answers 4

2

maybe just swapping two lines, because myApp IS your reference to your object: or try repeating until zero:

while (System.Runtime.InteropServices.Marshal.ReleaseComObject(myApp)>0)
       ;
myApp = null;
GC.Collect(); 
GC.WaitForPendingFinalizers ();
Sign up to request clarification or add additional context in comments.

1 Comment

sorry to hear, I always get rid of those objects via ReleaseComObject. btw, do you call 'ReleaseComObject', until it returns zero ?
0

Make sure, you don't have any other variable referring (holding a reference) to myApp.

Alternatively, a call to FinalReleaseComObject could also be of help.
Note: I looked at the docs & suggesting this as an alternative. Use your discretion when making a call to this method.

8 Comments

There is no variable referring to the myApp
Please post full code (if you can) or a sample app for one to figure out, why is it that the COM instance is not released? what does the COM object refer to? what type it is (written in vb6/vc++/delphi)?
@Pradeep: How do you ascertain that the COM class instance is not getting released?
there is one more application using the same com object. when i perform and finish with c# operation, i am unable to open the other application which has same com reference. it will be locked.
i can open other application before perform operation in c# code, once c# code operation done i am unable to open it. after closing the complete VS project opened, only then able to open other application. in real world user want to use want to use other application after the myapp operation either they have to kill the worker process or restart iis.
|
0

I've had a similar problem in the past and attempted all the things you've mentioned, but was unable to resolve it. Eventually I had to forcefully terminate my process to kill the COM object.

Comments

0

In order to be able to completely release the COM object you need to load it in a separate AppDomain which you can unload after you have called ReleaseComObject() and done other proper cleanup for your object.

This has proved to be the only reliable way for me in the past.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.