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.
An explanation for the commentor:
For the C++ bool type:
...
So, pretty much whatever value you use, you'll get a c++ boolean with the value true or false.