- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我知道 double free 或 corruption 错误通常是对 big 3 的违规,但在这种情况下,我找不到违规发生的地方。我有一个复制构造函数、析构函数和赋值运算符,适用于任何处理指针的东西。
在我的 .h 中,这是我的类实现:
class BST
{
public:
struct SequenceMap{
std::string astring;
std::vector<std::string> sequences;
//void setValue(std::string theString, std::string anotherString);
SequenceMap& operator=(const SequenceMap map);
void setValue(std::string theString, std::string anotherString);
SequenceMap(); //constructor no copy since no pointers
~SequenceMap();
};
struct BinaryNode{
SequenceMap item;
BinaryNode *left;
BinaryNode *right;
BinaryNode(SequenceMap i); //constructor
inline bool operator> (std::string t);
inline bool operator< (std::string t);
BinaryNode& operator=(const BinaryNode node) ;
~BinaryNode();
BinaryNode(const BinaryNode &otherNode);
};
BinaryNode *root;
int insert(SequenceMap &x, BinaryNode *&t, bool &ifdup);
BST();
~BST();
void BSTClear(BST::BinaryNode *t);
BST(const BST &otherTree);
BST& operator=(const BST tree);
};
我在我的 .cpp 中实现了我的构造函数、析构函数和赋值运算符:
BST::SequenceMap& BST::SequenceMap::operator=(const BST::SequenceMap map)
{
astring = map.astring;
sequences = map.sequences;
return *this;
}
inline bool BST::BinaryNode::operator<(std::string t){//does compare}
inline bool BST::BinaryNode::operator>(std::string t){//does compare}
BST::BinaryNode& BST::BinaryNode::operator=(const BST::BinaryNode node)
{
item = node.item;
if(node.left != nullptr)
left = new BST::BinaryNode(node.left->item);
else
left = nullptr;
if(node.right != nullptr)
right = new BST::BinaryNode(node.right->item);
else
right = nullptr;
return *this;
}
BST& BST::operator=(const BST tree){root = new BinaryNode(tree.root);}
BST::BinaryNode::BinaryNode(const BST::BinaryNode &otherNode){
item = otherNode.item;
if(otherNode.left != nullptr)
left = new BST::BinaryNode(otherNode.left->item);
else
left = nullptr;
if(otherNode.right != nullptr)
right = new BST::BinaryNode(otherNode.right->item);
else
right = nullptr;
}
BST::BinaryNode::BinaryNode(SequenceMap i){ item = i; left = nullptr; right = nullptr; }
BST::BinaryNode::~BinaryNode(){ delete &item; left = nullptr; right = nullptr; }
BST::BST(){root = nullptr;}
BST::BST(const BST &otherTree){root = new BinaryNode(otherTree.root->item);}
BST::~BST(){BSTClear(root);}
BST::SequenceMap::SequenceMap(){astring = "";}
BST::SequenceMap::~SequenceMap(){ delete &astring; delete &sequences;}
void BST::BSTClear(BST::BinaryNode*t){
if(t->left != nullptr)
BSTClear(t->left);
if(t->right != nullptr)
BSTClear(t->right);
delete t;
}
我使用 cout
来测试错误发生的位置,当我在我的 main.cpp 中的指示行上执行此操作时会发生错误:
while(getline(sequences,sequence) && getline(enzymes,enzyme))
{
BST::SequenceMap map = BST::SequenceMap;
map->setValue(sequence, enzyme);
sequenceTree->insert(map, sequenceTree->root, dup); //ON THIS LINE
}
在我的 .cpp 中插入函数:
int BST::insert(BST::SequenceMap &x, BST::BinaryNode *&t, bool &ifdup )
{
if(t == nullptr)
{
//std::cout<<"2"<<std::endl;
t = new BST::BinaryNode(x); //ON THIS LINE
//std::cout<<"1"<<std::endl;
}
//do more things
}
我不确定这是否被视为 MSCV,但我至少需要重现我的错误。
最佳答案
考虑您的 BinaryNode
赋值运算符。
BST::BinaryNode& BST::BinaryNode::operator=(const BST::BinaryNode node)
{
item = node.item;
if(node.left != nullptr)
left = node.left;
else
left = nullptr;
if(node.right != nullptr)
right = node.right;
else
right = nullptr;
return *this;
}
您最终还是会得到 BinaryNode
的两个实例,它们的 left
和 right
指针指向同一事物。当调用两个实例的析构函数时,它们都会释放指针并导致双重释放。
您需要做的是实际制作一个新拷贝 values 由 left
和 right
指针指向,而不是 < em>指针,或者有某种引用计数指针。
另请注意:如果原始值是 nullptr
nullptr
关于c++ - 双重自由或腐败(出)C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26417088/
一个特定文档不会更新,而同一查询会更新我查询的任何其他 _id: Category.findOneAndUpdate( {_id : req.params.id}, {parent :
我正在使用 ifstream 读取文件头。编辑:我被要求放置完整的最小程序,所以就在这里。 #include #include using namespace std; #pragma pack(
typedef struct { int *info; } row; struct { row* head; int len; int size; } list; in
在索引 block 时我在 debug.log 中有错误: 2016-11-14 15:14:52 UpdateTip: new best=000000000000038ad111e773453f17
我是一名优秀的程序员,十分优秀!