unordered_map segment fault

unordered_map segment fault

本文关键字:fault segment map unordered      更新时间:2023-10-16

我正在尝试使用c ++完成一个intepreter。原始版本效果很好。我尝试向其添加内存池和gc。内存池类 MemPool 包含指向 MemList 的指针,MemList 包含指向自由列表和忙列表的指针。它们是指向 MemBlock 的指针,MemBlock 包含一个用于分配块的 void * 指针。

我覆盖运算符 new 并删除语法树节点。运算符 new 仅用于 ASTree 基类,malloc 在其他情况下用作替代项。ASTree 是所有语法树节点的基类。

void *ASTree::operator new(size_t size){
cout<<"Using modified new operator!"<<endl;
void *buff=MemPool::getInstance()->alloc(size);
return buff;
}
void ASTree::operator delete(void *buff){
if(!MemPool::getInstance()->dealloc(buff))
throw MemoryError(curmodname,curline);
}

在声明节点中,我使用 unordered_map 来存储相关的子声明。

class DeclModule:public Declaration{
public:
DeclModule(const string &modname);
//    ~DeclModule();
int getDeclType();
void intepret();
string modname;
unordered_map<string, DeclModule *> modulelist;
unordered_map<string, DeclClass *> classlist;
unordered_map<string, DeclMethod *> methodlist;
DeclEntry *entry;
};

我发现程序在不同时间在不同的地方崩溃。我发现错误可能是由unordered_map引起的

DeclMethod *declmethod=methodParser();
declmodule->methodlist[declmethod->methodname]=declmethod;

有时 xcode 在 __nd = __bucket_list_[__chash] 中定位错误;

template <class _Key, class _Args>
_LIBCPP_INLINE_VISIBILITY
pair<typename __hash_table<_Tp, _Hash, _Equal, _Alloc>::iterator, bool>
__hash_table<_Tp, _Hash, _Equal, _Alloc>::__emplace_unique_key_args(_Key const& __k, _Args& __args)
#endif
{
size_t __hash = hash_function()(__k);
size_type __bc = bucket_count();
bool __inserted = false;
__next_pointer __nd;
size_t __chash;
if (__bc != 0)
{
__chash = __constrain_hash(__hash, __bc);
__nd = __bucket_list_[__chash];
if (__nd != nullptr)
{
for (__nd = __nd->__next_; __nd != nullptr &&
__constrain_hash(__nd->__hash(), __bc) == __chash;
__nd = __nd->__next_)
{
if (key_eq()(__nd->__upcast()->__value_, __k))
goto __done;
}
}
}

有时定位错误

__builtin_operator_delete(__ptr);

有时 xcode 告诉我我不能使用释放(指针或内存?记不清楚(。我确定在调用插入时会发生错误unodered_map。我想当我插入一个元素时,新的和删除被调用的。五月

需要你的帮助T-T,你能告诉我哪里有问题以及如何修改它吗

如果没有实际调试,很难回答这个问题,但我认为您的池分配器中存在错误。

我建议你运行像valgrind这样的调试工具,而不是解决方案,如下所示:在Xcode环境中,Valgrind相当于什么?

否则,如果你的代码在Linux上独立编译,你可以直接使用valgrind。

如果应用程序是多线程的,请确保池分配器是安全的。