C ++:如何解决在未知点处引起的第一次机会异常? [英] C++ : How can I solve a first-chance exception caused at an unknown point?

查看:74
本文介绍了C ++:如何解决在未知点处引起的第一次机会异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理的C ++项目在引发优先机会异常时终止.当我第一次尝试访问包含单个键值对的map<pair<int,int>, int>时,这会在Visual Studio 2008中的调试模式下发生.代码在逻辑上没有错.

A C++ project I'm working on terminates upon throwing a first-chance exception. This occurs in Visual Studio 2008 in Debug mode when I first try to access a map<pair<int,int>, int> which contains a single key-value pair. There is nothing logically wrong with the code.

我已经阅读了第一时间的例外情况,并了解它们可能并不总是有问题的.尽管如此,我还是尝试打破所有此类异常,并且按预期发现,生成了几个不会引起问题的异常.

I've read up on first-chance exceptions and understand they may not always be problematic. Nonetheless I tried breaking at all such exceptions, and as expected found several are generated which cause no issue.

我正在处理的类非常大,并且包含许多自定义内存分配.我推测其中某种原因导致了问题.但是,我花了几个小时试图找到一种方法来确定出了什么问题,但未能做到这一点.

The class I'm working on is very large and contains many custom memory allocations. I surmise that somehow one of these is causing the problem. However I've spent several hours attempting to find a way to identify what's going wrong, and have been unable to do so.

以下列出了第一个机会异常输出.这不是很有帮助!

The first-chance exception output is listed below. It isn't very helpful!

First-chance exception at 0x762cd09c in theapp.exe: 0xC0000005: Access violation reading location 0x6c696d00.
First-chance exception at 0x762cd09c in theapp.exe: 0xC0000005: Access violation reading location 0x6c696d00.
First-chance exception at 0x762cd09c in theapp.exe: 0xC0000005: Access violation reading location 0x6c696d00.
First-chance exception at 0x762cd09c in theapp.exe: 0xC0000005: Access violation reading location 0x6c696d00.
First-chance exception at 0x762cd09c in theapp.exe: 0xC0000005: Access violation reading location 0x6c696d00.
First-chance exception at 0x762cd09c in theapp.exe: 0xC0000005: Access violation reading location 0x6c696d00.
First-chance exception at 0x762cd09c in theapp.exe: 0xC0000005: Access violation reading location 0x6c696d00.
First-chance exception at 0x762cd09c in theapp.exe: 0xC0000005: Access violation reading location 0x6c696d00.
First-chance exception at 0x762cd09c in theapp.exe: 0xC0000005: Access violation reading location 0x6c696d00.
First-chance exception at 0x762cd09c in theapp.exe: 0xC0000005: Access violation reading location 0x6c696d00.
First-chance exception at 0x762cd09c in theapp.exe: 0xC0000005: Access violation reading location 0x6c696d00.
First-chance exception at 0x762cd09c in theapp.exe: 0xC0000005: Access violation reading location 0x6c696d00.
First-chance exception at 0x762cd09c in theapp.exe: 0xC0000005: Access violation reading location 0x6c696d00.
First-chance exception at 0x762cd09c in theapp.exe: 0xC0000005: Access violation reading location 0x6c696d00.
First-chance exception at 0x762cd09c in theapp.exe: 0xC0000005: Access violation reading location 0x6c696d00.
First-chance exception at 0x0050ae33 in theapp.exe: 0xC0000005: Access violation reading location 0x00000010.
Unhandled exception at 0x0050ae33 in theapp.exe: 0xC0000005: Access violation reading location 0x00000010.

我现在真的很挣扎,不确定如何进行.

I'm really struggling at this point, and am unsure how to proceed.

任何人都可以建议我如何解决这个问题,并准确确定出了什么问题吗?非常感谢您的建议.

Can anyone suggest how I might tackle this problem, and identify exactly what's going wrong? I'd be very grateful for your advice.

更新

这是相关的代码.调试器在嵌套的FOR中列出的第一个cout语句上中断:

Here's the related code. The debugger breaks on the first cout statement listed in the nested FOR:

        // Inside operator() :

        map<pair<int,int>,int> resultIdByStructIds;
        pair<int,int> spair (-1,-1); // Structure pair ids reusable reference.

        int nextMapEntryId = 0;
        int nextNumCandidates = 0;
        // For each remaining candidate.
        for (int ci = 0; ci < numCandidates; ) {
            // If candidate has been mapped or found not viable this mapping round,
            // move past it.
            if (candidatesDoneThisRound[ci] == currentMappingRoundId) {
                ++ci;
                continue;
            }

            Candidate candidate = candidates[ci];
            const int tId = candidate.tVertexId;
            const int pId = candidate.pVertexId;

            // Grab the result for this structure pair.
            // Create it if it doesn't exist.
            // Avoid copying as slight optimisation; simply
            // store pointer to true result instead.
            spair.first = tInfos[tId].structure->id;
            spair.second = pInfos[pId].structure->id;

            // DEBUG
            cout << "resultIdByStructIds size: " << resultIdByStructIds.size() << endl;
            for (map<pair<int,int>,int>::const_iterator ids_id = resultIdByStructIds.begin(); ids_id != resultIdByStructIds.end(); ++ids_id) {
                cout << ids_id->first.first << endl; // * Debugger breaks here.
                cout << ids_id->first.second << endl;
                cout << ids_id->second << endl;
                printf("Structures(%i,%i) => %i\n",ids_id->first.first,ids_id->first.second,ids_id->second);
            }
            //

            // code continues...

更新2

这是有关地图的鼠标悬停说明的图像;好像迈克尔·伯尔(Michael Burr)建议的那样损坏了.

Here's an image of the mouseover description for the map in question; it appears corrupted as Michael Burr suggested.

推荐答案

通常,要查明应用程序崩溃的代码位置,可以在调试/异常"下打开异常处理.在这种情况下,您将展开最后一个分支并选中访问冲突".当然,这将停止所有访问冲突,而不仅仅是不良访问(访问0x10).您可以通过在最后一个已知的时刻打开陷阱来最大程度地减少这种情况.

In general, to pinpoint the spot of code where the app crashes, you can turn on exception handling under Debug/Exceptions. In this case, you would expand the last branch and check Access Violation. Of course that will stop on all the access violations, and not just the bad one (accessing 0x10). You can minimize this by turning on the trap at the last known moment.

通常,您会发现一些内存使用错误.确定此类错误原因的最简单方法是使用BoundChecker之类的第三方工具,一旦损坏内存,它就会对您大喊大叫.缺少这一点,Raymond Chen的建议是正确的.找出错误的对象,然后使用监视窗口查看更改的时间.或者更有效地,使用数据断点功能可以使程序在特定地址的数据更改时停止.

Typically you will discover some memory usage error. The easiest way to determine the cause of this type of error is a third party tool like BoundChecker which will yell at you as soon as you corrupt the memory. Lacking that, Raymond Chen's advice is spot on. Figure out what object is wrong, and use the watch window to see when it changed. Or more efficiently, use the Data Breakpoint feature to have the program stop when the data at a particular address changes.

这篇关于C ++:如何解决在未知点处引起的第一次机会异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆