double free or corruption(out)C++

问题描述:

我知道一个double free或者corrupt错误通常是违反了big 3,但是在这种情况下,我找不到违规发生的地方。我有一个拷贝构造函数,析构函数和任何处理指针的赋值操作符。double free or corruption(out)C++

在我这里的.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,但我这是我需要重现我的错误最少。 Stack Trace

+1

使用的valgrind或调试器来查明问题。至少你应该能够在你的问题中包含堆栈跟踪。 – 2014-10-17 02:52:23

+0

@JohnZwinck有没有兼容Windows和C++ 11的兼容?我没有访问Linux机器atm – SemicolonExpected 2014-10-17 02:53:57

+0

您还没有包含BinaryNode构造函数。 – 2014-10-17 02:55:01

考虑您的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; 
} 

下,仍然以他们的leftright指针指向同一件事BinaryNode的两个实例。当两个实例的析构函数被调用时,它们将释放指针并导致一个双重空闲。

你需要做什么是由leftright指针,而不是指针实际上使一个新的副本指出,或有某种形式的引用计数指针。

另外请注意:你如果测试不增加任何价值,你只是分配nullptr如果原始值是nullptr

+0

我会如何深入复制这个?它是否必须像'left = new BST :: BinaryNode(left-> item)',并且会递归地创建一个与第一个相同的新的“子树”或将不起作用? – SemicolonExpected 2014-10-17 03:16:26

+1

是的,那是对的。如果你的拷贝构造函数不是'nullptr',你需要做的就是递归地调用自己的'left'和'right'节点。幸运的是,你已经有了你需要的测试:) – 2014-10-17 03:31:43

+0

其实你想'left = BST :: BinaryNode(node.left)',所以你调用了复制构造函数。 – 2014-10-17 03:34:14