Nevinyrral
Nevinyrral

Reputation: 17

Segmentation fault due possible loose pointers

I'm having some issues regarding a segment fault

Program received signal SIGSEGV, Segmentation fault.
0x0000000000403a62 in std::_Deque_iterator<float, float&, float*>::_Deque_iterator (this=0x7fffffffc5c0, __x=...)
    at /usr/include/c++/4.6/bits/stl_deque.h:136
136         _M_last(__x._M_last), _M_node(__x._M_node) { }
(gdb) up
#1  0x0000000000403a0f in std::deque<float, std::allocator<float> >::begin (this=0x64) at /usr/include/c++/4.6/bits/stl_deque.h:1010
1010          { return this->_M_impl._M_start; }
(gdb) up
#2  0x000000000040326f in std::deque<float, std::allocator<float> >::front (this=0x64) at /usr/include/c++/4.6/bits/stl_deque.h:1286
1286          { return *begin(); }
(gdb) up
#3  0x000000000040248c in std::queue<float, std::deque<float, std::allocator<float> > >::front (this=0x64)

at /usr/include/c++/4.6/bits/stl_queue.h:165 165 return c.front(); (gdb) up #4 0x0000000000402ee3 in KDTree::Node::Create (this=0x6251c0, coords=0x623ec0, limit=500) at KDTree.hxx:64 64 if( ( *itC ) -> front() > maxAbove ) maxAbove = ( *itC ) -> front();

Here's the piece of code

template< class T, class D >
void KDTree< T, D >::Node::
Create( Coords* coords, D limit )
{
    Coords* newCoordsBelowMedian = new Coords(); 
    Coords* newCoordsAboveMedian = new Coords();
    D maxAbove = 0,
    minAbove = 0,
    maxBelow = 0,
    minBelow = 0;
    this -> m_Coords = coords;
    this -> m_Median = GetMedian( *coords );
    typename Coords :: iterator itC = this -> m_Coords -> begin( ); 
    //Change of coordinates for next iteration
    for( ; itC != this -> m_Coords -> end( ); itC++ )
    {
        Dims* newDim = *itC;
        D value = newDim -> front( );
        newDim -> pop( );
        newDim -> push( value );
        if( newDim -> front() >= this -> m_Median )   newCoordsAboveMedian -> insert(  );
        else                                          newCoordsBelowMedian -> insert(  );
    }  

    typename Coords :: iterator itCA = newCoordsAboveMedian -> begin( );
    typename Coords :: iterator itCB = newCoordsBelowMedian -> begin( );

    minBelow = std::numeric_limits<D>::max();
    minAbove = std::numeric_limits<D>::max();
                    //Max radius
    for( ; itC != newCoordsAboveMedian -> end( ); itCA++ )                             
    {
      if( ( *itC ) -> front() > maxAbove ) maxAbove = ( *itC ) -> front();
      if( ( *itC ) -> front() < minAbove ) minAbove = ( *itC ) -> front();
    }
    for( ; itC != newCoordsBelowMedian -> end( ); itCB++ )
    {
        if( ( *itC ) -> front() > maxBelow ) maxBelow = ( *itC ) -> front();
        if( ( *itC ) -> front() > maxBelow ) minBelow = ( *itC ) -> front();
    }

    if( abs( maxAbove - minAbove ) < limit && newCoordsAboveMedian -> size() > 0 )
    {
        this -> m_R = new Node();
        this -> m_R -> Create( newCoordsAboveMedian, limit );
    }
    if( abs( maxBelow - minBelow ) < limit && newCoordsAboveMedian -> size() > 0 )
    {
        this -> m_L = new Node();
        this -> m_L -> Create( newCoordsBelowMedian, limit );
    }       
}    

I suspect is because pointers at the first for are being lost upon completion, however, I don't know any solution to this issue, any thoughts?

Upvotes: 1

Views: 195

Answers (1)

Dietmar K&#252;hl
Dietmar K&#252;hl

Reputation: 154005

It seems itC is an iterator for this -> m_Coords which got run to the end in the first loop. The same iterator is used to control the later loops. Did you mean this loop

 for( ; itC != newCoordsAboveMedian -> end( ); itCA++ )                             
            {
              if( ( *itC ) -> front() > maxAbove ) maxAbove = ( *itC ) -> front();
              if( ( *itC ) -> front() < minAbove ) minAbove = ( *itC ) -> front();
            }

To read

 for( ; itCA != newCoordsAboveMedian -> end( ); itCA++ )                             
            {
              if( ( *itCA ) -> front() > maxAbove ) maxAbove = ( *itCA ) -> front();
              if( ( *itCA ) -> front() < minAbove ) minAbove = ( *itCA ) -> front();
            }

... or, what I would do if I were to write this loop:

  for (typename Coords::iterator it = newCoordsAboveMedian->begin( ),
                                 end = newCoordsAboveMedian->end();
       it != end; ++it) {
              if( ( *it ) -> front() > maxAbove ) maxAbove = ( *it ) -> front();
              if( ( *it ) -> front() < minAbove ) minAbove = ( *it ) -> front();
            }

(likewise for the other loop).

Upvotes: 1

Related Questions