我在我的应用程序中使用Trhird派对工具。在某些系统上,我收到System.AccessViolation错误。尽管通过代码我无法再现问题。但在生产环境中,它确实会在某个时间重现。System.AccessViolation由于Marshal.StructureToPtr
我有下面的代码
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;
}
如果您提供“true”而不是false,会发生什么情况? – Aliostad 2012-03-27 13:27:41
显然你必须通过false,因为没有旧的结构要删除。 – 2012-03-27 23:13:31
你的应用程序的目标是什么?我希望它是x86给予ToInt32。 – 2012-03-27 23:14:52