You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

释放未分配指针报错排查:附Addition类getAdditiveTerms方法代码

Hey there, let's break down why you're hitting that "释放的指针未被分配" (freed pointer was not allocated) error and how to fix it.

Root Cause Analysis

The error almost certainly stems from improper memory ownership in your code. Looking at your snippet:

Expression* LL = obj1->getLeftSide();
terms.push_back(LL);
delete obj1->getLeftSide();

Here's the problem:

  • If getLeftSide() returns a pointer to a member variable of obj1 (that obj1 is responsible for deleting in its destructor), calling delete here leaves obj1 holding a dangling pointer. When obj1 tries to delete that pointer later, you get a double-free error, or if the pointer was never dynamically allocated (e.g., points to a stack object or literal), deleting it triggers undefined behavior (which manifests as this error).
  • You're also pushing the pointer into the terms vector, but deleting it immediately means the vector now holds a dangling pointer—any use of that pointer later will cause crashes.

Fixes to Try

1. Stop deleting pointers you don't own

If obj1 is supposed to manage the memory of its left/right side pointers, remove the delete calls entirely. The vector can hold references (or raw pointers for observation, not ownership) as long as obj1 remains valid while the vector is in use:

vector <Expression*> Addition :: getAdditiveTerms() {
    vector <Expression*> terms;
    if((obj1->getType() == "Addition" || obj1->getType() == "Subtraction") && 
       (obj2->getType() == "Addition" || obj2->getType() == "Subtraction")) {
        if(obj1->getLeftSide()->getType() == "Integer") {
            Expression* LL = obj1->getLeftSide();
            terms.push_back(LL);
            // Remove the delete line here—obj1 retains ownership
        }
        // ... rest of your code for right side and obj2
    }
    return terms;
}

2. Transfer ownership to the vector

If you want the terms vector to take full ownership of the pointers, you need to nullify the pointer in obj1 so it doesn't try to delete it later. Add a setter method to your Expression class to handle this:

// Inside your Expression class:
void setLeftSide(Expression* newLeft) {
    leftSide = newLeft;
}

// Modified getAdditiveTerms method:
if(obj1->getLeftSide()->getType() == "Integer") {
    Expression* LL = obj1->getLeftSide();
    terms.push_back(LL);
    obj1->setLeftSide(nullptr); // Tell obj1 it no longer owns this pointer
    // Now the vector is responsible for deleting LL later
}

Make sure obj1's destructor checks if the pointer is non-null before deleting it to avoid errors:

Expression::~Expression() {
    if(leftSide != nullptr) delete leftSide;
    if(rightSide != nullptr) delete rightSide;
}

3. Use smart pointers to eliminate manual memory management

The safest way to avoid these errors is to replace raw pointers with std::unique_ptr or std::shared_ptr to handle ownership automatically. This removes the need for manual delete calls entirely:

// Update your Expression class to use unique_ptr:
class Expression {
private:
    std::unique_ptr<Expression> leftSide;
    std::unique_ptr<Expression> rightSide;
public:
    std::unique_ptr<Expression>& getLeftSide() { return leftSide; }
    std::unique_ptr<Expression>& getRightSide() { return rightSide; }
    // ... other methods
};

// Modified getAdditiveTerms:
vector <std::unique_ptr<Expression>> Addition :: getAdditiveTerms() {
    vector <std::unique_ptr<Expression>> terms;
    if((obj1->getType() == "Addition" || obj1->getType() == "Subtraction") && 
       (obj2->getType() == "Addition" || obj2->getType() == "Subtraction")) {
        if(obj1->getLeftSide()->getType() == "Integer") {
            // Transfer ownership from obj1 to the vector using std::move
            terms.push_back(std::move(obj1->getLeftSide()));
            // No delete needed—unique_ptr handles cleanup automatically
        }
        // ... rest of your code
    }
    return terms;
}

Quick Additional Checks

  • Verify that every pointer you try to delete was allocated with new. Deleting stack-allocated objects or literals is undefined behavior.
  • Add null checks before accessing pointers returned by getLeftSide()/getRightSide() to avoid crashes from null references.

内容的提问来源于stack exchange,提问作者Baker

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 07:16:58