COM Releasing .NET Object

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Michael Tissington

    #1

    COM Releasing .NET Object

    I have an application written in C++ (NOT .NET) which creates an instance of
    a vb .net object.

    My application releases the .net object when it has finished using it.
    However .NET GC is not destroying the object until my application closes
    which can be minutes or hours later.

    How can my C++ application force the .NET object to be TOTALY destroyed when
    I release it.

    Thanks.

    --
    Michael Tissington

    Protect your privacy with reusable webcam cover stickers and personalize your MacBook with adhesive-free designs—no residue. Custom branding available.




  • TDC

    #2
    Re: COM Releasing .NET Object

    Is the VB.NET object one you wrote? If it is, then implement a Dispose
    method that performs all cleanup explicitly. That way you don't have
    to worry about the garbage collector.

    Tom

    Comment

    • Tim Anderson

      #3
      Re: COM Releasing .NET Object

      "Michael Tissington" <michael@nospam .com> wrote in message
      news:eL%23ajiNr FHA.1984@tk2msf tngp13.phx.gbl. ..
      [color=blue]
      > How can my C++ application force the .NET object to be TOTALY destroyed
      > when I release it.[/color]

      According to .NET and COM guru Adam Nathan, you should call Dispose from the
      COM client in the same way as you would in a .NET app.

      If you can't modify the COM client, you can implement a custom marshaler
      that calls Dispose when the CCW's ref count reaches zero, but it is complex.

      See a post by David Stucki in microsoft.publi c.dotnet.framew ork.interop.

      Tech blog



      Comment

      • Michael Tissington

        #4
        Re: COM Releasing .NET Object

        I'm writing both the .NET object and the COM client.

        So how does the COM client call Dispose on the .NET Object ?

        Does the .NET object need to impalement Dispose ?

        The problem seems to be related to the COM client using IConnectionPoin t to
        ask for Events.
        Then when the client frees the Connection the .NET object does not release
        the client until the .NET object has been really destroyed (at the moment I
        have resorted to GC.Collect() to force this).

        --
        Michael Tissington

        Protect your privacy with reusable webcam cover stickers and personalize your MacBook with adhesive-free designs—no residue. Custom branding available.



        "Tim Anderson" <timjand@hotmai l.com> wrote in message
        news:usvYiDXrFH A.4072@TK2MSFTN GP09.phx.gbl...[color=blue]
        > "Michael Tissington" <michael@nospam .com> wrote in message
        > news:eL%23ajiNr FHA.1984@tk2msf tngp13.phx.gbl. ..
        >[color=green]
        >> How can my C++ application force the .NET object to be TOTALY destroyed
        >> when I release it.[/color]
        >
        > According to .NET and COM guru Adam Nathan, you should call Dispose from
        > the COM client in the same way as you would in a .NET app.
        >
        > If you can't modify the COM client, you can implement a custom marshaler
        > that calls Dispose when the CCW's ref count reaches zero, but it is
        > complex.
        >
        > See a post by David Stucki in microsoft.publi c.dotnet.framew ork.interop.
        >
        > Tech blog
        > http://www.itwriting.com/blog/
        >
        >[/color]


        Comment

        • Tim Anderson

          #5
          Re: COM Releasing .NET Object


          "Michael Tissington" <michael@nospam .com> wrote in message
          news:%23aZJutar FHA.1684@TK2MSF TNGP14.phx.gbl. ..[color=blue]
          > I'm writing both the .NET object and the COM client.
          >
          > So how does the COM client call Dispose on the .NET Object ?[/color]

          Just implement iDisposable in the .NET object. Write code for the Dispose
          method that cleans up resources. Then have the COM client call Dispose.

          Note this won't actually destroy the object, but it means you can release
          resources.
          [color=blue]
          > Does the .NET object need to impalement Dispose ?[/color]

          Yes.
          [color=blue]
          > (at the moment I have resorted to GC.Collect() to force this).[/color]

          Purists will say not to use GC.Collect() but in a situation like this I
          wouldn't hesitate if it solves the problem.

          Tim
          Tech blog



          Comment

          • Jay B. Harlow [MVP - Outlook]

            #6
            Re: COM Releasing .NET Object

            Tim,
            | Purists will say not to use GC.Collect() but in a situation like this I
            | wouldn't hesitate if it solves the problem.
            I would question if it actually solves the problem or simply *masks* the
            problem.

            By masking the problem I mean making it appear the problem is solved.

            I hope you agree that masking the problem is not really the same as solving
            the problem. Although masking the problem sometimes is the only "viable"
            solution.

            For details on when to call GC.Collect see Rule #1 at:
            [color=blue][color=green]
            >> http://blogs.msdn.com/ricom/archive/...29/271829.aspx
            >> http://blogs.msdn.com/ricom/archive/.../02/40780.aspx[/color][/color]

            Just remember that "the collector is self-tuning so don't mess with it"!

            Hope this helps
            Jay


            "Tim Anderson" <timjand@hotmai l.com> wrote in message
            news:eoaHgigrFH A.260@TK2MSFTNG P11.phx.gbl...
            |
            | "Michael Tissington" <michael@nospam .com> wrote in message
            | news:%23aZJutar FHA.1684@TK2MSF TNGP14.phx.gbl. ..
            | > I'm writing both the .NET object and the COM client.
            | >
            | > So how does the COM client call Dispose on the .NET Object ?
            |
            | Just implement iDisposable in the .NET object. Write code for the Dispose
            | method that cleans up resources. Then have the COM client call Dispose.
            |
            | Note this won't actually destroy the object, but it means you can release
            | resources.
            |
            | > Does the .NET object need to impalement Dispose ?
            |
            | Yes.
            |
            | > (at the moment I have resorted to GC.Collect() to force this).
            |
            | Purists will say not to use GC.Collect() but in a situation like this I
            | wouldn't hesitate if it solves the problem.
            |
            | Tim
            | Tech blog
            | http://www.itwriting.com/blog/
            |
            |


            Comment

            Working...