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 / March 2005

Tip: Looking for answers? Try searching our database.

Memory leak?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Steven Kilby - 26 Mar 2005 09:13 GMT
Hi,

I'm not sure, but I may have found a memory leak between vb6 and .net.  Of
course, it is possible that I just don't understand something correctly or I
did something wrong.  In any event, what I've done is create a C# class
library that exposes an interface through COM.  If I instantiate this
interface in vb6 it is never released.  I included a finalize method and it
is never called.  If I instantiate the same interface in C++ it is released
as expected and the finalize method is called.  I understand that the
execution of the finalize method is non-deterministic.  I've tried waiting
for extended periods and it is just never called from the vb app.  I've
boiled it down to a very simple example.  Below is a C# server, a C++
client, and a VB6 client.  The finalize method is called everytime when the
C++ app exits and it is never called when the VB6 client exists.  If you
compile these the VB6 app is a console app, so you'll have to run 'editbin
/SUBSYSTEM:CONSOLE' on the exe.  If any can explain the behavior, I would
appreciate it.

Thanks
Steven

================= C#
using System;

using System.Runtime.InteropServices;

namespace MemLeak

{

/// <summary>

/// Summary description for IMemLeak.

/// </summary>

[InterfaceType(ComInterfaceType.InterfaceIsDual)]

[GuidAttribute("E2F25E23-40D4-4f9b-BC2F-0D6BA932DA31")]

[ComVisible(true)]

[CLSCompliant(false)]

public interface IMemLeak

{

void DummyMethod();

}

/// <summary>

/// Summary description for MemLeakObj.

/// </summary>

[ClassInterface(ClassInterfaceType.None)]

[GuidAttribute("66BAA837-CCA4-4a07-AC6C-8EFBE22928BB")]

[ProgId("MemLeak.MemLeakObj.1")]

[ComVisible(true)]

public class MemLeakObj : IMemLeak

{

public MemLeakObj()

{

Console.WriteLine("MemLeakObj: Constructor");

}

~MemLeakObj()

{

Console.WriteLine("MemLeakObj: Finalizer");

}

#region IMemLeak Members

public void DummyMethod()

{

Console.WriteLine("MemLeakObj: DummyMethod");

}

#endregion

}

}

================== VB6

Option Explicit

Public Const STD_OUTPUT_HANDLE = -11&

Public Declare Function stdout Lib "kernel32" Alias "GetStdHandle" _
(Optional ByVal Handletype As Long = STD_OUTPUT_HANDLE) As Long

Public Declare Function WriteFile Lib "kernel32" _
(ByVal hFile As Long, ByVal lpBuffer As Any, ByVal nNumberOfBytesToWrite As
Long, _
lpNumberOfBytesWritten As Long, Optional ByVal lpOverlapped As Long = 0&) As
Long
Sub Main()

   Dim sWriteBuffer As String
   Dim lBytesWritten As Long
   Dim hStdOut As Long

   sWriteBuffer = "MemLeak VB Client" & vbCrLf
   hStdOut = stdout()
   WriteFile hStdOut, sWriteBuffer, Len(sWriteBuffer), lBytesWritten

   Dim o As Object
   Set o = CreateObject("MemLeak.MemLeakObj.1")
   Set o = Nothing
End Sub

==================== C++

// This is the main project file for VC++ application project

// generated using an Application Wizard.

#include "stdafx.h"

#include <objbase.h>

#define CRTDBG_MAP_ALLOC

#include <stdlib.h>

#include <crtdbg.h>

#using <mscorlib.dll>

using namespace System;

int _tmain()

{

// TODO: Please replace the sample code below with your own.

Console::WriteLine(S"MemLeak C++ Client");

::CoInitialize(NULL);

CLSID clsid;

HRESULT hr = ::CLSIDFromProgID(L"MemLeak.MemLeakObj.1", &clsid);

IDispatch *p = NULL;

hr = ::CoCreateInstance(clsid, NULL, CLSCTX_INPROC_SERVER, IID_IDispatch,
(void**)&p);

p->Release();

_CrtDumpMemoryLeaks();

::CoUninitialize();

return 0;

}
Daniel Petersson, Cefalo - 29 Mar 2005 11:43 GMT
There is no such simple leak! The only reason for not finalizing the
object during execution is that you keep a reference to it. Therefore
you probably miss a Release from VB. (set to Nothing)

To gurarantuee the the object is created you shall always make a
call to it. For example Remoted objects are not created until the first
call is made. (I'm not certain how the runtime behaivs with COM but
I assume that the lazy paradigm is common to avoid overhead)

The _CrtDumpMemoryLeaks will no show any leaks on the managed
heap!

// Daniel

> Hi,
>
[quoted text clipped - 175 lines]
>
> }

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.