C++ : How can I solve a first-chance exception caused at an unknown point?

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

我正在处理的一个 C++ 项目在抛出第一次异常时终止。当我第一次尝试访问 map<pair<int,int>, int> 时,这发生在处于 Debug模式的 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

这是相关 map 的鼠标悬停说明图片;正如 Michael Burr 所建议的那样,它似乎已损坏。

enter image description here

最佳答案

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

通常您会发现一些内存使用错误。确定此类错误原因的最简单方法是使用 BoundChecker 等第三方工具,一旦您破坏了内存,它就会对您大喊大叫。缺乏这一点,Raymond Chen 的建议是正确的。找出错误的对象,并使用监 window 口查看它何时更改。或者更有效地,使用数据断点功能让程序在特定地址的数据更改时停止。

关于C++ : How can I solve a first-chance exception caused at an unknown point?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8555573/

相关文章:

c++ - const重载而不必写两次函数

c# - Java 的 System.getProperty ("user.dir"的 .NET 等价物是什么)?

c++ - Debug和Release模式下的AES加密问题

.NET - 用于密集调试的第一次机会异常监听器?

c++ - 奇怪的 C++ 字符串连接行为

c++ - Boost::shared_ptr 使用这个实例化

c++ - 区分静态数组,指针和结构的C++模板

asp.net - 安装 Visual Studio Express 2010

c# - 抑制第一次机会异常

vb.net - 第一次机会异常(exception)