双重免费或腐败(外出)C ++

时间:2014-10-17 02:44:19

标签: c++ class memory constructor runtime-error

我知道双重免费或损坏错误通常违反了大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,但我这是我需要重现我的错误。 Stack Trace

1 个答案:

答案 0 :(得分:3)

考虑您的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的{​​{1}}和left指针的两个实例都指向同一个东西。当调用两个实例的析构函数时,它们将释放指针并导致双重释放。

您需要做的是实际制作rightleft指针所指向的的新副本,而不是指针,或者有一些引用计数指针。

另请注意:如果原始值为right

,您的if测试不会添加任何值,因为您只需指定nullptr