0

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?

  • 1
    How does `Volume`s destructor look like? – πάντα ῥεῖ Oct 10 '15 at 09:27
  • Destructor of Volume is empty. All args in Volume are static. In this function I'm deleting only the pointer to Volume not the object itself. – Olzhas Turar Oct 10 '15 at 09:34
  • Well, we cannot tell anything, if you're not showing the relevant code in context. – πάντα ῥεῖ Oct 10 '15 at 09:36
  • @OlzhasTurar FYI: That loop can be replaced with this: `auto iter = std::find(m_volumes.begin(), m_volumes.end(), _volume); if ( iter != m_volumes.end()) m_volumes.erase( iter );` – PaulMcKenzie Oct 10 '15 at 09:37
  • 1
    @OlzhasTurar You are mismanaging pointers elsewhere in your application. It is this code that finally shows the damage. – PaulMcKenzie Oct 10 '15 at 09:40
  • I've replaced contents of the function to the proposed code and get the same error at the "erase" operator. – Olzhas Turar Oct 10 '15 at 09:43
  • I'm using visual studio 2010 debugging and it shows that all the arguments evolved in this code have correct contents. List has only 4 elements and one of them is equal to the "_volume" object by [ptr] and other fields. – Olzhas Turar Oct 10 '15 at 09:58
  • Debug then moves me to xutility file's line 177 and say that some _Iterator_base12 **_Pnext contain CXX0030: Error: expression cannot be evaluated – Olzhas Turar Oct 10 '15 at 10:01

2 Answers2

2

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());
Community
  • 1
  • 1
TheOperator
  • 5,936
  • 29
  • 42
-2

I found the place generating the error. But I have no idea why.

Whole my code is implementing as heirs of abstract Event class

class Event
{
public:
    Event( ) { }
    ~Event( ) { }

    virtual void Implement( void ) abstract;
    virtual void ReturnColors( void );      
private:
};

There are deque of pointers to Events

std::deque< Event * >   m_triangulationEvents;

Which are used following way

void Delaunay3D::nextEvent( )
        {
            if( !m_triangulationEvents.empty( ) )
            {
                m_prevEvent->ReturnColors( );

                Event * cur = m_triangulationEvents.front( );
                m_triangulationEvents.pop_front( );
                cur->Implement( );

                delete m_prevEvent;
                m_prevEvent = cur;
            }
        }

The problem were at

delete m_prevEvent;

string. Commenting it or using

m_prevEvent.~Event( );

results in well working application. Thanks for everyone.

P.S. @TheOperator thanks for mentioning freed dynamic memory

  • -1 -- Your fix is not a fix. You are not supposed to call the destructor explicitly like that. The one glaring issue with your code is that your `Event` class lacks a virtual destructor. – PaulMcKenzie Oct 10 '15 at 11:47
  • Yes after adding virtual keyword it works well – Olzhas Turar Oct 10 '15 at 12:32