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

客户端的结构体是

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

服务器C++定义的是

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

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

        ///<summary>系统消息</summary>
        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto, Pack = 1)]
        public struct CMD_CM_SystemMessage
        {
            ///<summary>消息类型</summary>
            public ushort wType;
            ///<summary>消息长度</summary>
            public ushort wLength;
            ///<summary>消息内容</summary>
            [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?

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

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,424评论 0 10
  • 通常认为,家庭最难处的是婆媳关系。把婆媳当成一对天敌。甚至在家庭,用现在一个流行词语说,婆媳间玩起你死我活的“零和...
    蔡喜源阅读 706评论 0 2
  • 早晨,我与太太到辛庄念佛堂念佛, 念佛一个小时, 走出念佛堂,天还未亮, 我闻着一股稻香的味道,来到了稲田, 上个...
    蒋坤元阅读 1,155评论 11 26
  • 1.时间怎么花掉的? 早上打扫办公室下午上班看书《月总结》晚上亲子溜冰看书 2.今天收支如何? 开心吗? 早餐6元...
    晴空00阅读 224评论 0 0
  • 各位家长朋友,高一年级定于明天下午3点35分,第七节结束后放假,9月10日下午3点半前在教室坐好,打扫卫生...
    高飞1阅读 343评论 1 2