c++ - 析构函数中的段错误

标签 c++ list segmentation-fault destructor

当我尝试执行我的项目时遇到段错误。在运行 Btree 类的析构函数的 main 末尾,它调用 Node 类的析构函数。然后在 Word 类的析构函数调用中我得到了错误。并且 list.tcc 打开 (~Btree -> ~Node() -> ~Word() (error): list.tcc:)

光标在这一行给出错误:

list.tcc:

_Node* __cur = static_cast<_Node*>(this->_M_impl._M_node._M_next);

这个错误的原因是Word类中的列表吗?

类代码简写如下:

class Btree{
private:
...
Node *root;
...
public:...
~Btree(){delete[] root;};

};

class Node{
...
Word *words;
Node **children;
...
    ~Node(){delete [] words; delete []children;};
};

class Word{
public:
string word;
list<Couple> couple;

    Word(){};
    ~Word(){};
};

class Couple{
...
public:
....
    ~Couple(){};
 };

最佳答案

你是如何分配你的root的?我个人的猜测是你分配它使用

root = new Node();

如果您尝试将非数组对象释放为数组对象,您将得到未定义的行为。实际上,它会在节点开始之前使用这个词,并假设它是一个元素计数并销毁该数量的元素。因为只有一个,所以一定不能很好地工作。你可能想要

delete root;

关于c++ - 析构函数中的段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8773354/

相关文章:

c - 计算循环期间出现段错误

c++ - 为什么很多 Allocator 函数是可选的?

c++ - C++虚函数的重载解析——引用与指针

c++ - C++ Linux 中损坏的双链表

Python 将列表的第一个元素与其余元素一起压缩

c - 终端 : segmentation fault 11 while reading file and counting the amount of vowels

Linux C 和 C++ : what else should I be logging when handling signals like SIGSEGV?

c++ - 写入输出文件

Python列表差异

java - 从数组列表添加元素到 List<List<String>>