I have a 3D vector defined like this...
std::vector<std::vector<std::list<Object*> > > m_objectTiles;
I have this code...
void ObjectManager::AddObject( Object *object ) {
m_objects.push_back( object );
m_objectTypes.insert( std::make_pair(
ObjectAttorney::GetType( object ), object ));
int x = ObjectAttorney::GetTileX( object );
int y = ObjectAttorney::GetTileY( object );
m_objectTiles[x][y].push_back( object ); // SEG FAULT HERE
}
that receives this error 0x0805ccdb in std::vector<std::list<Object*, std::allocator<Object*> >, std::allocator<std::list<Object*, std::allocator<Object*> > > >::operator[] ( this=0x8157758, object=0x8173f30) at /usr/include/c++/4.4/bits/stl_vector.h:611 { return *(this->_M_impl._M_start + __n); }
I changed it to this to test it...
void ObjectManager::AddObject( Object *object ) {
m_objects.push_back( object );
m_objectTypes.insert( std::make_pair(
ObjectAttorney::GetType( object ), object ));
int x = ObjectAttorney::GetTileX( object );
int y = ObjectAttorney::GetTileY( object );
std::list<Object*> *l = &m_objectTiles[x][y];
if ( l ) { // SEG FAULT HERE
l->push_back( object );
} else {
std::cout << "List null.\n";
}
}
which just gives an error message saying where the seg fault occured ObjectManager::AddObject (this=0x81577a0, object=0x8165760) at ObjectManager.cpp:381 if ( l ) {
Why would a seg fault occur when testing for a null pointer? Obviously operator [] is returning something corrupted or invalid. Not sure what the problem is here. Any help is appreciated. Thanks.