简体   繁体   中英

double free or corruption (out) C++

I know a double free or corruption error is usually a violation of the big 3, but in this instance, I cannot find where the violation occurs. I have a copy constructor, destructor, and assignment operator for anything that deals with pointers.

In my .h here is my class implementation:

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);
};

I implemented my constructors, destructors and assignment operators in my .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;
}

I used cout to test for where the error occurs and it occurs when I do this in my main.cpp on the indicated line:

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
}

and in my insert function in my .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
 }

I'm not sure if this is considered MSCV, but I this is the least I need to reproduce my error. 堆栈跟踪

Consider your BinaryNode assignment operator.

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;
}

You still end up with both instances of the BinaryNode having their left and right pointers pointing to the same thing. When the destructors of the two instances are called, they will both free up the pointers and cause a double free.

What you need to do is to actually make a new copy the values pointed to by the left and right pointers, not the pointers , or have some sort of reference counted pointer.

Also note: Your if tests don't add any value as you are just assigning nullptr if the original value is nullptr

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM