I am using Trhird party tool in my application. On some systems i get the System.AccessViolation error. Although, through code i am not able to reproduce the issue. But in the production environment it does reproduce at some time.
I have the doubt on the following code
public static IntPtr TestMarshalToPointer(object value, System.Type type, int length)
{
int offset = 0;
int size = Marshal.SizeOf(type) * length;
IntPtr buffer;
try
{
buffer = Marshal.AllocHGlobal(size);
for (var index = 0; index < length; index++)
{
Marshal.StructureToPtr(value, new IntPtr(buffer.ToInt32() + offset), false);
// Its written on MSDN that passing false can lead to memory leak. Please guide , should i need to pass true and how it will affect**
offset += Marshal.SizeOf(type);
}
}
catch
{
buffer = (IntPtr)null;
}
return buffer;
}