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;
}