C ++:如何解决在未知点引起的第一次机会异常?

时间:2011-12-18 23:07:15

标签: c++ visual-studio-2008 first-chance-exception

我正在努力的C ++项目在抛出第一次机会异常时终止。当我第一次尝试访问包含单个键值对的map<pair<int,int>, int>时,会在Visual Studio 2008中以调试模式发生这种情况。代码没有任何逻辑错误。

我已经阅读了第一次机会异常,并了解它们可能并不总是有问题。尽管如此,我试图打破所有这些例外情况,并且正如预期的那样,发现了几个没有引起问题的情况。

我正在处理的类非常大,包含许多自定义内存分配。我猜测其中一个导致了这个问题。然而,我花了几个小时试图找到一种方法来识别出现了什么问题,并且无法这样做。

下面列出了第一次机会异常输出。这不是很有帮助!

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.

我现在真的很挣扎,我不确定如何继续。

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

更新

这是相关代码。调试器在嵌套FOR:

中列出的第一个cout语句中断
        // 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建议的那样,它似乎已经腐败了。

enter image description here

1 个答案:

答案 0 :(得分:9)

通常,为了查明应用程序崩溃的代码点,您可以在Debug / Exceptions下打开异常处理。在这种情况下,您将展开最后一个分支并检查访问冲突。当然,这将停止所有访问冲突,而不仅仅是坏的访问(访问0x10)。您可以通过在最后一个已知时刻打开陷阱来最小化这一点。

通常,您会发现一些内存使用错误。确定此类错误原因的最简单方法是使用像BoundChecker这样的第三方工具,一旦损坏内存,它就会对你大喊大叫。由于缺乏这一点,Raymond Chen的建议很明显。弄清楚哪个对象出错了,并使用监视窗口查看它何时发生变化。或者更有效的是,使用数据断点功能使程序在特定地址的数据发生变化时停止。