I have a following c++ code:
typedef std::list< Volume >::iterator pVolume;
typedef std::list< pVolume >::iterator ppVolume;
void Node::delVolume( pVolume _volume )
{
for( ppVolume it = m_volumes.begin( ); it != m_volumes.end( ); )
if( (*it) == _volume )
{
it = m_volumes.erase( it );
break;
}
else
it++;
}
it gets an error
Unhandled exception at 0x009a3c79 in Delone3D.exe: 0xC0000005: Access violation reading location 0xfeeefef2.
exactly when erasing. Debug shows that neither "it" nor "_volume" is null pointer.
What other reasons this may occur for?
The code you show is correct, it seems like there's a problem elsewhere in your application. The memory pattern 0xfeeefef2
(a few addresses above 0xfeeefeee
) indicates freed dynamic memory, see here.
You can massively simplify your code, by the way:
// for std::list, as in your example
m_volumes.remove(_volume);
// for std::vector and std::deque
auto itr = std::remove(m_volumes.begin(), m_volumes.end(), _volume);
m_volumes.erase(itr, m_volumes.end());