【发布时间】:2011-12-18 23:07:15
【问题描述】:
我正在处理的 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 所建议的那样,它似乎已损坏。
【问题讨论】:
-
导致它终止的不是第一次机会异常,而是未处理的异常。
-
我不知道 MSVS,但不知何故“访问冲突”让我怀疑“代码没有问题”......
-
您说“代码在逻辑上没有任何问题。”。到目前为止,所有迹象都表明代码有问题。您能否向我们展示代码行(以及相关的前面行)。
-
也许这个以前的解决方案可以解决你的问题stackoverflow.com/questions/6371713/…
-
好吧,一些理由:“逻辑上”显然是主观的;我没有发布代码,因为这个类有几千行。我假设由于未处理异常中引用的地址与前面的第一次机会异常相同,因此我应该这样描述它。无论如何,我会发布一些代码和/或利用您提供的链接,Jamie。感谢大家的建议,稍后我会再次发表评论。
标签: c++ visual-studio-2008 first-chance-exception