Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / June 2005

Tip: Looking for answers? Try searching our database.

COM interop und garbage collection

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Guido Kraus - 20 Jun 2005 11:31 GMT
Hello,

I'm trying to connect a Delphi COM client with our .NET server. Everything
seems to work fine, except that COM-created .NET objects are not properly
garbage collected, e.g. the C# destructors won’t get invoked.

Especially when marshalling .NET streams to COM IStreams which do not
provide a close or dispose mechanism this behaviour causes many problems.

Although trying hard I couldn’t find any similar problem descriptions in the
internet. Are we doing something wrong or is it a fact that COM-created .NET
instances won’t get finalized? And is there any kind of best practice how to
deal with that problem?

Thanks for your ideas,
Guido
"Peter Huang" [MSFT] - 21 Jun 2005 07:39 GMT
Hi

Based on my experience, the COM client -->CCW-->.NET code.
But the CCW will not call the dispose on the .NET library automatically,
also the Finalizer queue(i.e. the deconstructor of .NET class) is managed
by GC, it took the responsibility to handle the release issue.

So for now,  I suggest you implement the IDisposable interface on your .net
class and call the dispose explicitly in your com client to do the release
the necessary resource.
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace DecntorComServer
{
    [ClassInterface(ClassInterfaceType.AutoDual)]
    public class MyClass : IDisposable
    {
        public MyClass()
        {
            Debug.WriteLine("MyClass Constructor");
        }
        public void TestCall()
        {
            Debug.WriteLine("Call in");
        }
        ~MyClass()
        {
            Debug.WriteLine("MyClass DeConstructor");
//We will see that even use the class in managed code, we still need to
call GC.Collect() to enable the Finalize/deconstructor
//Also we did not recommend call GC.Collect ourselves, because that may
lead to performance hit.

        }
        #region IDisposable ³ÉÔ±

        public void Dispose()
        {
            Debug.WriteLine("Calling Dispose");
        }

        #endregion
    }
}

Best regards,

Peter Huang
Microsoft Online Partner Support

Signature

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.