0
您好,我有这个结构访问冲突在Marshal.StructureToPtr
[StructLayout (LayoutKind.Sequential)]
public struct Transfer_packet
{
public int _packet_type; // 0 is action 1 is data
public int _packet_len; // length of data
public byte[] _data;//;= new byte[DataLenght];
public void fill()
{
}
public byte[] deserialize()
{
int size = System.Runtime.InteropServices.Marshal.SizeOf(this);
byte[] arr = new byte[size];
IntPtr ptr = System.Runtime.InteropServices.Marshal.AllocHGlobal(size);
System.Runtime.InteropServices.Marshal.StructureToPtr(this, ptr, true); // error raised
System.Runtime.InteropServices.Marshal.Copy(ptr,arr,0,size);
System.Runtime.InteropServices.Marshal.FreeHGlobal(ptr);
return arr;
}
}
我试图结构的内容字节数组转换为发送该网络上和其他电脑,但在我上面提到的代码恢复它我得到错误试图读取或写入受保护的内存。这通常表明其他内存已损坏。我不是为什么每个东西都看起来不错,但元帅正试图访问一个受保护的内存,我怎么能将一个结构数据转换为它的块内的字节数组我已经在C++中完成了一个简单的memcpy,但在c#导致错误!
感谢
它带来了内存泄漏;( – saeed 2013-03-09 15:20:33
我的问题是,我想在网络中传输数据包格式发送数据块,并在其中的VC++ – saeed 2013-03-09 15:22:10
其他应用程序收到@ saeed你怎么检测这个“内存泄漏”? – 2013-03-09 15:37:01