Following piece of code is dumping a core sporadically while calling vector.erase(itr). Could you pls point out if I'm doing something wrong here?
std::vector<Ntfy>::iterator itr = NtfyVector.begin();
for ( ; itr != NtfyVector.end(); )
{
if (certaion condition) {
itr = NtfyVector.erase(itr);
continue;
} else {
itr++;
}
}
Following are the GDB Traces:
(gdb) where
#0 0x000000000048cea5 in __gnu_cxx::new_allocator::destroy (this=0x7f0890, __p=0x7adbbf0) at /usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/ext/new_allocator.h:107
#1 0x000000000048da34 in std::vector >::erase (this=0x7f0890, __position={_M_current = 0x7adbc40}) at /usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/vector.tcc:115
I m using g++ (GCC) 4.4.7 20120313 (Red Hat 4.4.7-3).
Here is the complete code:
void NtfyHandle (void) {
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++;
}
}
}
haNtfyIter
? And why aren't you usingstd::remove_if
? And why aren't you showing your actual code? – Benjamin Lindleyremove_erase_idiom
– billzif (certaion condition)
. If you have a piece of code which is not working, and you don't know what is causing it not to work, then how do you know which parts are irrelevant? We appreciate efforts to reduce the code to a minimum example, but make sure that the problem still exists with that reduced example, otherwise we're just chasing ghosts. sscce.org – Benjamin Lindley