【发布时间】:2015-10-10 09:25:19
【问题描述】:
我有以下 c++ 代码:
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++;
}
出现错误
Delone3D.exe 中 0x009a3c79 处未处理的异常:0xC0000005:访问 违规读取位置0xfeeefef2。
正好在擦除时。调试显示“it”和“_volume”都不是空指针。
这可能还有什么其他原因?
【问题讨论】:
-
Volumes 的析构函数是什么样子的? -
Volume 的析构函数为空。 Volume 中的所有参数都是静态的。在这个函数中,我只删除指向 Volume 的指针,而不是对象本身。
-
好吧,如果你没有在上下文中显示相关代码,我们什么也说不出来。
-
@OlzhasTurar 仅供参考:该循环可以替换为:
auto iter = std::find(m_volumes.begin(), m_volumes.end(), _volume); if ( iter != m_volumes.end()) m_volumes.erase( iter ); -
@OlzhasTurar 您在应用程序的其他地方对指针管理不善。正是这段代码最终显示了损坏情况。