以下代码在调用vector.erase(itr)时偶尔转储核心。您能否指出我在这里做错了什么?
std::vector<Ntfy>::iterator itr = NtfyVector.begin();
for ( ; itr != NtfyVector.end(); )
{
if (certaion condition) {
itr = NtfyVector.erase(itr);
continue;
} else {
itr++;
}
}
以下是 GDB 跟踪:
(gdb) 在哪里
#0 0x000000000048cea5 in __gnu_cxx::new_allocator::destroy (this=0x7f0890, __p=0x7adbbf0) at /usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../包括/c++/4.1.2/ext/new_allocator.h:107
#1 0x000000000048da34 in std::vector >::erase (this=0x7f0890, __position={_M_current = 0x7adbc40}) 在 /usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../。 ./../include/c++/4.1.2/bits/vector.tcc:115
我正在使用 g++ (GCC) 4.4.7 20120313 (Red Hat 4.4.7-3)。
这是完整的代码:
无效NtfyHandle(无效){
std::vector<Ntfy>::iterator itr = NtfyVector.begin();
for ( ; itr != NtfyVector.end(); )
{
PF_BOOL bRetry = PF_FALSE;
switch (itr->GetOperation()) {
case PF_SERVICE_ADD:
{
bRetry = LISwithState(itr->GetServiceName(), itr->GetNewState(), itr->GetInvokeId());
}
break;
case PF_SERVICE_REM:
{
liPsPlatform->SendInvokeResponse(itr->GetInvokeId(), PF_STATUS_SUCCESS);
}
break;
default: break;
}
if (PF_FALSE == bRetry) {
// Delete the notification from the vector since retry isn't required
itr = NtfyVector.erase(itr);
continue;
} else {
itr++;
}
}
}