Reputation: 2963
I'm developing an application which involves native C++ to C# interop via a C++/CLR wrapper.
I'm having trouble with the following operation, which causes a memory leak:
MyObject data = (MyObject)Marshal.PtrToStructure(ptr, typeof(MyObject));
Marshal.StructureToPtr(data, ptr, false);
(NOTE: I realise that i'm not actually doing anything to "data" at this time, so this is superfluous.)
Memory usage continues rising until the app crashes due to the system running out of memory. When I remove this code, this doesn't happen. It's not the garbage collector since a) it should collect when the system gets low on memory and b) i've tried forcing it with GC.Collect().
In fact, I've narrowed the leak down to the StructureToPtr command.
I'm unable to set the third parameter to "true", since the memory was allocated by native C++, and C# considers this "protected" memory that cannot be freed.
I've checked that the populated Data struct is intact, has valid data, and is the same size as the equivalent native struct.
In my mind, this is what should be happening:
My native structure referenced by ptr is marshalled and copied to the "data" managed structure
The manage structure is copied back to the same memory referenced by ptr.
I can't see how this can cause a memory leak, because it's exactly the same size of structure, being copied back into the same memory space. But clearly it does, removing the code plugs the leak.
Is there some mechanic here which i'm not understanding correctly?
Edit: As requested, here are the declarations for "MyObject".
C#:
[StructLayout(LayoutKind.Sequential)]
public struct MyObject
{
[MarshalAs(UnmanagedType.I1)]
public bool ParamOne;
[MarshalAs(UnmanagedType.I1)]
public bool ParamTwo;
[MarshalAs(UnmanagedType.I1)]
public bool ParamThree;
[MarshalAs(UnmanagedType.I1)]
public bool ParamFour;
[MarshalAs(UnmanagedType.I1)]
public bool ParamFive;
[MarshalAs(UnmanagedType.I1)]
public bool ParamSix;
[MarshalAs(UnmanagedType.R4)]
public float ParamSeven;
[MarshalAs(UnmanagedType.R4)]
public float ParamEight;
[MarshalAs(UnmanagedType.R4)]
public float ParamNine;
public Vector2f ParamTen;
public Vector2f ParamEleven;
[MarshalAs(UnmanagedType.LPWStr)]
public string ParamTwelve;
[MarshalAs(UnmanagedType.LPWStr)]
public string ParamThirteen;
[MarshalAs(UnmanagedType.LPWStr)]
public string ParamFourteen;
public IntPtr ParamFifteen;
public IntPtr ParamSixteen;
}
C++:
struct MyObject
{
public:
bool ParamOne;
bool ParamTwo;
bool ParamThree;
bool ParamFour;
bool ParamFive;
bool ParamSix;
float ParamSeven;
float ParamEight;
float ParamNine;
Vector2f ParamTen;
Vector2f ParamEleven;
wchar_t * ParamTwelve;
wchar_t * ParamThirteen;
wchar_t * ParamFourteen;
void * ParamFifteen;
void * ParamSixteen;
};
The definition of Vector2f is as follows:
[StructLayout(LayoutKind.Sequential)]
public struct Vector2f
{
[MarshalAs(UnmanagedType.R4)]
float x;
[MarshalAs(UnmanagedType.R4)]
float y;
}
Upvotes: 3
Views: 3580
Reputation: 18463
You have pointers to strings in your structure. Those pointers are assigned in your unmanaged code (using new wchar_t[<a number>]
), right? When marshaling those pointers to managed code, marshaler creates managed string and fills them with contents of the unmanaged character arrays. When marshaling them back to unmanaged code, the marshaler copies the whole struct content, including the character pointers, assigning them new values (allocating memory for each string using CoTaskMemAlloc()
). That's what the third parameter of the Marshal.StructureToPtr
is for. If it is set to true, the marshaler tries to deallocates the memory pointed by character pointers (using CoTaskMemFree()
). If you have allocated the memory for character pointers using new
operator, you cannot set that parameter to true, and by marshaling back to unmanaged, you lose the pointers to allocated memories (marshaller overwrites them with new values). And since you are not deallocating the memory allocated by marshaler, you end up with memory leak.
The best option to deal with this situation is this:
Marshal strings as pointers and use Marshal.PtrToStringUni()
to convert them to strings:
[StructLayout(LayoutKind.Sequential)]
public struct MyObject
{
[MarshalAs(UnmanagedType.I1)]
public bool ParamOne;
[MarshalAs(UnmanagedType.I1)]
public bool ParamTwo;
[MarshalAs(UnmanagedType.I1)]
public bool ParamThree;
[MarshalAs(UnmanagedType.I1)]
public bool ParamFour;
[MarshalAs(UnmanagedType.I1)]
public bool ParamFive;
[MarshalAs(UnmanagedType.I1)]
public bool ParamSix;
[MarshalAs(UnmanagedType.R4)]
public float ParamSeven;
[MarshalAs(UnmanagedType.R4)]
public float ParamEight;
[MarshalAs(UnmanagedType.R4)]
public float ParamNine;
public Vector2f ParamTen;
public Vector2f ParamEleven;
public IntPtr ParamTwelve; // <-- These are your strings
public IntPtr ParamThirteen; // <--
public IntPtr ParamFourteen; // <--
public IntPtr ParamFifteen;
public IntPtr ParamSixteen;
}
and the marshaling:
MyObject data = (MyObject)Marshal.PtrToStructure(ptr, typeof(MyObject));
var str1 = Marshal.PtrToStringUni(data.ParamTwelve);
var str2 = Marshal.PtrToStringUni(data.ParamThirteen);
var str3 = Marshal.PtrToStringUni(data.ParamFourteen);
Marshal.StructureToPtr(data, ptr, false);
Upvotes: 4