xcode 错误:被释放的指针未分配

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3487870/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 19:38:16  来源:igfitidea点击:

error: pointer being freed was not allocated

c++xcodepointersoperator-overloading

提问by Jordan

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's 并且<<运算符为正确的输出而过载等......

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方法。

采纳答案by Maciej Hehl

I can't see the constructor for the PolygonNodeclass. Is the link_pointer initialized to null on creation? If not, that may be the problem manifesting itself in the error you get. You have to make sure, the link_pointers in the PolygonNodeinstances get initialized to null. Define appropriate constructors.

我看不到PolygonNode该类的构造函数。link_创建时指针是否初始化为空?如果不是,那可能是出现在您得到的错误中的问题。您必须确保实例中的link_指针PolygonNode被初始化为 null。定义适当的构造函数。

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 Polygonclass.

由编译器自动合成的复制构造函数只会复制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, plyinside the operator has pointers pointing to the same list, the argument passed by value to the operator owns. plybehaves like it owned the list too and the list gets destroyed when plygoes out of scope. The original argument is left with dangling pointers.

这使用了复制构造函数。如果是自动合成的,ply运算符内部有指向同一个列表的指针,按值传递给运算符的参数拥有。ply表现得就像它也拥有列表一样,并且列表在ply超出范围时会被销毁。原始参数留下悬空指针。

You should define correct copy constructor.

您应该定义正确的复制构造函数。

You should also consider taking the argument in the assignment operator by const reference. 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. I don't think it's right, but I guess it's just for testing now.

您还应该考虑通过常量引用获取赋值运算符中的参数。我看不出有理由按价值来接受它。也许你有一个,但即使你有,你也可以在定义正确的复制构造函数之前临时更改它以测试运算符。在您的运营商中,您应该检查自分配。我现在能看到的只是向旧的Polygon. 我不认为这是正确的,但我想现在只是为了测试。

回答by M. Sadeq H. E.

I think the problem is the link_variable, it's not allocated in your example and never used...

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

回答by Puppy

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。