简体   繁体   English

错误:释放的指针未分配

[英]error: pointer being freed was not allocated

I am trying to overload the assignment operator to do a deep copy of a polygon object, the program compiles but I am getting an error toward the end that I want to clear up. 我试图使赋值运算符重载,以对多边形对象进行深层复制,程序可以编译,但是在最后要清除的地方出现错误。 Below is the relevant code, if you think I need to add more please just post a comment. 以下是相关代码,如果您认为我需要添加更多内容,请发表评论。 Assume the proper #include 's and that the << operator is overloaded for proper output etc... 假设正确的#include<<操作符已重载以实现正确的输出等...

The error is: malloc: * error for object 0x1001c0: pointer being freed was not allocated * set a breakpoint in malloc_error_break to debug. 错误是:malloc: *对象0x1001c0错误:未分配要释放的指针*在malloc_error_break中设置一个断点进行调试。

//Polygon.h
// contains two classes PolygonNode and Polygon
class PolygonNode //Used to link points in a polygon so that they can be iterated through in order
{
public:
...
methods etc
...
private:
Point pt_; // the points in the polygon are made using the Point class
PolygonNode* link_ ; // pointer to the next point in the polygon
};

class Polygon // Connects points and forms a polygon { public: ... Polygon& operator= (Polygon ply); void Polygon::addPoint(const Point &p); // methods etc ... private: int numPoints_; bool closed_polygon_; PolygonNode* first_ ; // points to the first point of the polygon PolygonNode* last_ ; // points to the last point of the polygon };

//Polygon.cpp
...
PolygonNode::~PolygonNode()
{
    delete link_ ; // possible problem area
}

Polygon::~Polygon() { delete first_ ; // possible problem area last_ = NULL ; }

void Polygon::addPoint(const Point &p) { PolygonNode* ptr ; ptr = new PolygonNode(p) ; if( last_ != NULL ) last_->setLink(ptr) ; last_ = ptr ; if( first_ == NULL ) first_ = last_ ; numPoints_++ ; } Polygon& Polygon::operator= (const Polygon ply) { for (int i = 0; i < ply.numPoints()-1; i++) { addPoint(ply.getPoint(i)); } if (ply.isClosed()) { closePolygon(); } else { addPoint(ply.getPoint(ply.numPoints()-1)); } return this; } void Polygon::addPoint(const Point &p) { PolygonNode ptr ; ptr = new PolygonNode(p) ; if( last_ != NULL ) last_->setLink(ptr) ; // sets the last pointer to the new last point last_ = ptr ; if( first_ == NULL ) first_ = last_ ; numPoints_++ ; } ...

//main.cpp
Polygon ply;
...
        Point pt0(0,0);
        Point pt1(1,1);

    ply.addPoint(pt0);

    cout << "ply = " << ply << endl;
    Polygon newply;

    newply = ply; // use of the assignment operator

    cout << "Polygon newply = ply;" << endl;
    cout << "newply = " << newply << endl;
    cout << "ply = " << ply << endl;

    newply.addPoint(pt1);
    cout << "newply.addPoint(Point(0,0)); " << endl;

    cout << "newply = " << newply << endl;
    cout << "ply = " << ply << endl;

...

I have read elsewhere that this is possibly due to a bug in OS 10.6 or Xcode 3.2 if there is a workaround could someone please give me detailed instructions for how to do the workaround, I do not have a lot of experience with Xcode. 我在其他地方读过,这可能是由于OS 10.6或Xcode 3.2中的错误引起的,如果有解决方法,有人可以给我详细的说明,以解决该问题,但我对Xcode经验不足。

Edited: added parts of code that use delete , notice that it is being used in the destructors for Polygon and PolygonNode 编辑:添加了使用delete的代码部分,请注意,它在Polygon和PolygonNode的析构函数中使用

Edited: added the part of the code where link_ is allocated, setLink is a simple setter method. 编辑:添加了分配link_的部分代码,setLink是一个简单的setter方法。

I can't see the constructor for the PolygonNode class. 我看不到PolygonNode类的构造函数。 Is the link_ pointer initialized to null on creation? 创建时link_指针是否初始化为null? If not, that may be the problem manifesting itself in the error you get. 如果不是这样,那可能是在您得到的错误中表现出来的问题。 You have to make sure, the link_ pointers in the PolygonNode instances get initialized to null. 您必须确保将PolygonNode实例中的link_指针初始化为null。 Define appropriate constructors. 定义适当的构造函数。

Do you have a copy constructor defined for your polygon class? 您是否为多边形类定义了复制构造函数? I can't see one in the code posted, but maybe you just didn't paste it and you have one. 我在发布的代码中看不到一个,但也许您只是没有粘贴而已。 If not, that is one of possible sources of serious problems. 如果不是,那可能是严重问题的根源之一。

The copy constructor, that gets synthesized automatically by the compiler will just copy the pointers in the Polygon class. 编译器自动合成的复制构造函数将只复制Polygon类中的指针。

Your assignment operator takes the argument by value 您的赋值运算符按值接受参数

Polygon& operator= (Polygon ply);

This makes use of the copy constructor. 这利用了复制构造函数。 If it's the automatically synthesized one, ply inside the operator has pointers pointing to the same list, the argument passed by value to the operator owns. 如果是自动合成的,则操作符内部的ply将具有指向同一列表的指针,按值传递给操作符的参数将拥有。 ply behaves like it owned the list too and the list gets destroyed when ply goes out of scope. ply行为也像它拥有该列表一样,并且当ply超出范围时,该列表将被销毁。 The original argument is left with dangling pointers. 原始参数保留有悬空指针。

You should define correct copy constructor. 您应该定义正确的副本构造函数。

You should also consider taking the argument in the assignment operator by const reference. 您还应该考虑通过const引用在赋值运算符中使用参数。 I don't see a reason to take it by value. 我认为没有理由重视它。 Maybe you have one, but even if you do, you can change it temporarily, to test the operator, before you define correct copy constructor. 也许您有一个,但是即使您有一个,也可以在定义正确的副本构造函数之前临时更改它以测试操作符。 In your operator you should check for self-assignment. 在您的操作员中,您应检查自我分配。 All I can see now is adding new nodes to the old Polygon . 我现在看到的就是在旧的Polygon添加新节点。 I don't think it's right, but I guess it's just for testing now. 我认为这是不对的,但我想现在仅用于测试。

我认为问题是link_变量,它在您的示例中没有分配,并且从未使用过...

You should never, ever use raw pointers unless in a dedicated class, normally. 通常,除非在专用类中,否则永远不要使用原始指针。 Change them to a smart pointer (auto or shared will do in this case) and stop having to free your own memory -> problem solved. 将它们更改为智能指针(在这种情况下将使用自动或共享指针),然后不再需要释放自己的内存->问题已解决。 Edit: 编辑:

The smarter option is just to use a std::list or std::vector. 聪明的选择是只使用std :: list或std :: vector。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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