Access Violation异常崩溃

今天在项目测试中,Unity Editor发生了崩溃就闪退了,什么都没有看到
再次测试在多次创建房间的时候,有可能发生崩溃
猜测是结构体的问题,因为采用了非托管代码
崩溃日志一般是这样的

Unity Editor [version: Unity 5.6.5f1_2cac56bf7bb6]

mono.dll caused an Access Violation (0xc0000005)
  in module mono.dll at 0033:5a74a6c7.

Error occurred at 2018-07-03_164316.
D:\Program Files\Unity5.6\Editor\Unity.exe, run by Administrator.
63% memory in use.
8161 MB physical memory [2988 MB free].
10337 MB paging file [1862 MB free].
134217728 MB user address space [134215577 MB free].
Read from location ffffffff caused an access violation.

崩溃的太突然,截图都截不了,想找是哪个结构体导致的问题比较麻烦
还好我有日志记录系统,通过查看日志记录log文件,崩溃最后个消息是CMD_CM_SystemMessage
ps:第二种方式是通过查看editor log看是什么情况下发生崩溃
https://docs.unity3d.com/Manual/LogFiles.html
windows系统文件在C:\Users\username\AppData\Local\Unity\Editor\Editor.log

客户端的结构体是

        ///系统消息
        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto, Pack = 1)]
        public struct CMD_CM_SystemMessage
        {
            ///消息类型
            public ushort wType;
            ///消息长度
            public ushort wLength;
            ///消息内容
            public string szString;
        }

服务器C++定义的是

//系统消息
struct CMD_CM_SystemMessage
{
    WORD                            wType;                              //消息类型
    WORD                            wLength;                            //消息长度
    TCHAR                           szString[1024];                     //消息内容
};

这里明显有问题,因为这个代码是以前生成的
C++转到C#结构体string必须加上[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 1024)]
重新生成后

        ///系统消息
        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto, Pack = 1)]
        public struct CMD_CM_SystemMessage
        {
            ///消息类型
            public ushort wType;
            ///消息长度
            public ushort wLength;
            ///消息内容
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 1024)]
            public string szString;
        }

再测试了下,暂时没什么问题

查了一下什么是Access Violation

https://forum.unity.com/threads/access-violation-error.272136/

I would highly recommend downloading memtest86 (I believe it's http://www.memtest86.com/) and do a system test on your RAM. Just in case you have a bad stick, just to rule it out. The exception is happening because it's trying to read from an address location in memory that doesn't exist or is protected and the app doesn't have access to it.

This is a bit that makes me scratch my head a bit:

D:\RiftHalloween\test19.exe, run by Steve.
21% memory in use.
0 MB physical memory [0 MB free].
0 MB paging file [0 MB free].
0 MB user address space [3355 MB free].
Read from location 0001c960 caused an access violation.

Why is it claiming that there's no physical memory or page file?

大概意思是:之所以会出现异常,是因为它试图从内存中不存在或受保护的地址位置读取,并且应用程序无法访问该地址。(应用程序没有访问它的权限。)

你可能感兴趣的:(Access Violation异常崩溃)