0
votes

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++;
    }
}

}

1
What is haNtfyIter? And why aren't you using std::remove_if? And why aren't you showing your actual code?Benjamin Lindley
sorry for the typo. It’s an actual code. I have tried replacing weird name of variables and irrelevant code for the sake of simplicity and for preciseness.user2981699
use remove_erase_idiombillz
No, it's not actual code. You don't have a statement that says if (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.orgBenjamin Lindley
here is the complete code:user2981699

1 Answers

1
votes

Seems like there's nothing to do with the given loop but perhaps something's wrong with Ntfy::~Ntfy destructor? 'erase-remove' idiom allows to have a lot neater code by the way.