×

Loading...
Ad by
  • 最优利率和cashback可以申请特批,好信用好收入offer更好。请点链接扫码加微信咨询,Scotiabank -- Nick Zhang 6478812600。
Ad by
  • 最优利率和cashback可以申请特批,好信用好收入offer更好。请点链接扫码加微信咨询,Scotiabank -- Nick Zhang 6478812600。

Anyone has expereince Com Interop in .NET using c#? I got some problems when I try to release a COM object using Marshal.ReleaseComObject.

I used two Com interop object in my method and one of them has to call some function to do some initilizeing by passing in the other com object. I declared the object as following:

B2K_Server2.clsAppBroker Broker = new B2K_Server2.clsAppBroker();
B2K_Server2.clsBLL_RequisitionClass objReq = new B2K_Server2.clsBLL_RequisitionClass();

objReq.Init(ref Broker);

I release them like this:

Marshal.ReleaseComObject(Broker);
Marshal.ReleaseComObject(objReq);

However, after the process is finished, I found the object objReq is released but Broker is not released. I think the problem is the way to declare the variable is different . Borker is using the original class from COM and the objReq is declared using the class the RCW generated. However, I cannot decalre the broker in the same way otherewise the
objReq.Init(ref Broker) will generate an error because of the different type of argument.
Anyone know how I can solve this problem?

Thanks in advance!
Report

Replies, comments and Discussions:

  • 工作学习 / 专业技术讨论 / Anyone has expereince Com Interop in .NET using c#? I got some problems when I try to release a COM object using Marshal.ReleaseComObject.
    I used two Com interop object in my method and one of them has to call some function to do some initilizeing by passing in the other com object. I declared the object as following:

    B2K_Server2.clsAppBroker Broker = new B2K_Server2.clsAppBroker();
    B2K_Server2.clsBLL_RequisitionClass objReq = new B2K_Server2.clsBLL_RequisitionClass();

    objReq.Init(ref Broker);

    I release them like this:

    Marshal.ReleaseComObject(Broker);
    Marshal.ReleaseComObject(objReq);

    However, after the process is finished, I found the object objReq is released but Broker is not released. I think the problem is the way to declare the variable is different . Borker is using the original class from COM and the objReq is declared using the class the RCW generated. However, I cannot decalre the broker in the same way otherewise the
    objReq.Init(ref Broker) will generate an error because of the different type of argument.
    Anyone know how I can solve this problem?

    Thanks in advance!
    • I think that is because the Broker is ref, which can not be released in this way.
      • Yes. I think that is the problem. I don't know how I can release a Com object like that. Do you have any good suggestion.
    • I don't think 'ReleaseComObject' really matters here. The interface must be leaked somewhere else.
      • And I don't think you need to call "ReleaseComObject" unless you 1. want the objects to be released in a certain order. 2. someone statically holds a reference of your object somewhere in your code.
        • I am not sure if I have to. In my code, I have to create the object in page load. Sometimes I got the error: The callee (server [not server application]) is not available and disappeared; all connections are invalid.
          This happens very random.So I think it is better to release the object immedately rather than wait for Garbage collection to release it.
          • That's sounds like an automation error, as the message states, you are making calls on a already released object.
            I would suggest to investigate when and why the object you are calling become invalid.