我试图在C++中使用STL列表,并且遇到了一个我无法理解的奇怪异常。STL列表异常
该列表被定义为list<ASTNode*> m_stats;
和ASTNode*
是一类。当我尝试通过调用
ASTNode *node = new ASTNode();
m_stats.push_back(node);
它抛出以下异常的添加元素:
Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_PROTECTION_FAILURE at address: 0x00000004
0x91c20fe7 in std::_List_node_base::hook()
我尝试使用gdb和插入检测值进行调试,是不是null,它到底是什么它应该是..
回溯是:
#0 0x91c20fe7 in std::_List_node_base::hook()
#1 0x0000a9fb in std::list<ASTNode*, std::allocator<ASTNode*> >::_M_insert (this=0x180344, __position={_M_node = 0x0}, [email protected]) at stl_list.h:1152
#2 0x0000aa27 in std::list<ASTNode*, std::allocator<ASTNode*> >::push_front (this=0x180344, [email protected]) at stl_list.h:743
#3 0x0000aa45 in ASTStatements::addStatement (this=0x180340, stat=0x180410) at ast.h:277
我缺少的东西?
编辑:添加类源
class ASTStatements : public ASTNode
{
list<ASTNode*> m_stats;
public:
ASTStatements() {}
ASTStatements(list<ASTNode*> stats)
{
std::copy(stats.begin(), stats.end(), m_stats.begin());
}
ASTStatements(const ASTStatements &other)
{
std::copy(other.m_stats.begin(), other.m_stats.end(), m_stats.begin());
}
ASTStatements &operator= (const ASTStatements &other)
{
if (&other != this)
{
std::copy(other.m_stats.begin(), other.m_stats.end(), m_stats.begin());
}
}
ASTStatements *clone()
{
return new ASTStatements(*this);
}
u8 type()
{
return 0;
}
const char *generateASM()
{
list<ASTNode*>::iterator it = m_stats.begin();
while (it != m_stats.end())
{
((ASTNode*)*it)->generateASM();
++it;
}
}
void addStatement(ASTNode *stat)
{
m_stats.push_front(stat);
}
u8 typeCheck()
{
return 0;
}
};
我把它用在一个语法野牛文件处理多个语句(没有找到更好的办法来处理非终端项目的泛型列表)在此方式:
statements:
statement { if ($$ == null) $$ = new ASTStatements(); ((ASTStatements*)$$)->addStatement($1); } statements { $$->generateASM(); }
;
在此先感谢
问题不在于您发布的任何代码行中。显然,你的'list'在此之前已经被破坏了。 – 2009-10-31 04:13:27
堆栈跟踪与发布的源代码不一致(push_back vs push_front等) – 2009-10-31 04:20:28
您的程序没有抛出异常,而是您的操作系统正在使您的程序摆脱其带有分段错误的痛苦。 – 2009-10-31 05:29:37