Interop Memory Problem

B

bbembi_de

Hello,

I use a C++ Dll in .Net 1.1 with Dllimport.
I call one C++ method in a loop and the memory of my .Net application
rises proportional.

What am I doing wrong calling this method?

Simplified it's like this:

[StructLayout(LayoutKind.Sequential)]
public struct MyStruct {
[MarshalAs(UnmanagedType.LPStr)] public string name;
public int media;
}

Test 1:

[DllImport("ToolkitApiDll.dll", ExactSpelling=true)]
public static extern int GetData(IntPtr mystruct);

IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(MyStruct)));
GetData(ptr);
MyStruct m = (MyStruct) Marshal.PtrToStructure(ptr, typeof(MyStruct));
GetData(ptr);
Marshal.FreeHGlobal(ptr);


Test 2:

[DllImport("ToolkitApiDll.dll", ExactSpelling=true)]
public static extern int GetData(ref MyStruct mystruct);

MyStruct m = new MyStruct();
GetData(ref m);


Both ways I get the data but the memory rises.

Any ideas?
Thanks.

bye bembi
 
M

Mattias Sjögren

Are you sure the mempry leak (if there is one) is on the managed side,
and not in the C++ library?


Mattias
 
B

bbembi_de

Mattias said:
Are you sure the mempry leak (if there is one) is on the managed side,
and not in the C++ library?

The memory leak could be on the C++ side. That is a COM Server.
But I just want to be sure the leak isn't in my C# application.

bye bembi
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top