我有一个像这样定义的 3D 矢量...
std::vector<std::vector<std::list<Object*> > > m_objectTiles;
我有这个代码...
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
}
在 std::vector 中收到此错误 0x0805ccdb >、std::allocator > > >::operator[] ( this=0x8157758, object=0x8173f30) 位于 /usr/include/c++/4.4/bits/stl_vector.h:611 { return *(this->_M_impl._M_start + __n); 我将
其更改为此来测试它...
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";
}
}
这只是给出一条错误消息,说明在 ObjectManager.cpp:381 发生段错误的位置 ObjectManager::AddObject (this=0x81577a0, object=0x8165760) if ( l ) {
为什么测试空指针时会出现段错误?显然,运算符 [] 返回的内容已损坏或无效。不确定这里出了什么问题。任何帮助表示赞赏。谢谢。
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.
发布评论
评论(2)
出于性能原因,
std::vector
的[]
不进行范围检查。显然,如果x
或y
超出范围,则第二种变体没有帮助。添加这样的检查:
很难从引用的代码中判断,但可能您希望 std::vector 自动增长。不会的。为此,您需要类似的内容:
在访问
m_objectTiles[x][y]
之前。std::vector
's[]
for performance reasons doesn't do range checks. Obviously that second variant doesn't help ifx
ory
are out of range.Add a check like that:
It is hard to judge from the quoted code, but it might be that you want std::vector to grow automatically. It will not. For that you would need something like that:
before accessing the
m_objectTiles[x][y]
.您的问题最可能的原因是 ObjectAttorney::GetTileX 和 ObjectAttorney::GetTileY 返回超出范围的值,您检查过它们吗?
崩溃转储将 if 语句指示为崩溃站点的原因可能是旧的调试数据,只需重建您的项目即可。
The most probable cause for your problem is that ObjectAttorney::GetTileX and ObjectAttorney::GetTileY are returning out-of-range values, have you checked them?
The reason for the crash dump to indicate the if statement as the crash site is probably old debugging data, just rebuild your project.