memory leak when calling unmanaged code from managed code in Windows 7

When I call an unmanaged C++ code from my C# code, I seem to have some kind of a memory leak.
The C++ reads data from a file using ifstream.read, and writes it to a Vector.

This happens only after upgrading to Windows 7, doesn't happen on Vista, but if I use a version of the native dll that was compiled on Vista, it doesn't change anything!
If I run the same C++ code directly, without the managed interope, there is no memory leak!
If I run the managed process, but within the vshost process, there is no memory leak!

Here's the call signature:

        [DllImport(DllPath, CharSet = CharSet.Unicode)]
    [return: MarshalAs(UnmanagedType.I1)]
    public static extern bool MyMethod(
        int x, 
        string  y, 
        string  z, 
        bool    v, 
        bool    w);

and the native one:

MyDll_Export bool APIENTRY MyMethod(
int x,
const wchar_t*  y, 
const wchar_t*  z,
bool v,
bool w)

When I call it from C++, I call it like this:

MyMethod(1, L"My String 1", L"My String 2", true, true)

When I look at the performance counters for managed and unmanaged memory, I see that all of the memory comes from the unmanaged code.
Considering that the marshaling is pretty simple, I don't understand why there is a difference between calling the C++ directly or through C#.
I also don't know why would this happen only on Windows 7 (both Windows installations had .net 3.5 SP1).

Does anyone have an idea what's the reason for this?

Also if anyone knows of a native memory profiling tool that works on Window 7, I'd be glad to know (for now I've just printed to console all explicit memory allocation and there are no differences).


I'm sure the problem is related to marshaling the C# data types to their C++ counter parts. Since you are marshaling the return value bool to a signed 1 byte value, maybe you should do the same to the function arguments? The C# bool type is 4 bytes, maybe you are leaking there?

Also, specifying the unmanaged type for the strings may help.

[DllImport(DllPath, CharSet = CharSet.Unicode)]
[return: MarshalAs(UnmanagedType.I1)]
public static extern bool MyMethod(
        int x,
        [MarshalAs(UnmanagedType.LPWStr)]
        [In] string y,
        [MarshalAs(UnmanagedType.LPWStr)]
        [In] string z,
        [MarshalAs(UnmanagedType.I1)]
        bool v,
        [MarshalAs(UnmanagedType.I1)]
        bool w);

An explanation for the commentor:

For the C++ bool type:

In general, a zero or null-pointer value is converted to false, any other value is converted to true.

...

The 1998 C++ Standard Library defines a specialization of the vector template for bool. The description of the class indicates that the implementation should pack the elements so that every bool only uses one bit of memory.

So, pretty much whatever value you use, you'll get a c++ boolean with the value true or false.


Unfortunately once you involve strings, no marshalling is simple.

We're going to need some more data in order to help you track down this problem. Can you provide the following

  • Native Method Signature
  • How is the memory for the strings managed in native code?
  • Perhaps the C++ sample where you use the API?
  • EDIT

    Try the following signature. This tells the CLR not to marshal memory in both directions but instead only pass the data in.

        [DllImport(DllPath, CharSet = CharSet.Unicode)]
        [return: MarshalAs(UnmanagedType.I1)]
        public static extern bool MyMethod(
                int x, 
                [In] string  y, 
                [In] string  z, 
                bool    v, 
                bool    w);
    

    当发现我的内存泄漏时,我发现CLR Profiler的使用很有帮助。

    链接地址: http://www.djcxy.com/p/57034.html

    上一篇: 使用vs2010将C ++ / CLI项目更改为另一个不是4.0的框架

    下一篇: 在Windows 7中从托管代码调用非托管代码时发生内存泄漏