本文介绍了错误:释放的指针未分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我试图重载赋值运算符做一个多边形对象的深层副本,程序编译,但我得到一个错误,结束,我想要清除。下面是相关代码,如果你认为我需要添加更多,请发表评论。假设正确的 #include ,并且<< 错误是:malloc: *对象的错误0x1001c0:释放的指针未分配 * 在malloc_error_break中设置断点调试。 // Polygon.h //包含两个类PolygonNode和Polygon 类PolygonNode //用于链接多边形中的点,以便它们可以按顺序迭代通过 { public: ... 方法等。 .. private: Point pt_; //多边形中的点是使用Point类 PolygonNode * link_; //指向多边形中下一个点的指针}; 类多边形//连接点和形式多边形 { public: ... Polygon& operator =(Polygon ply); void Polygon :: addPoint(const Point& p); //方法等 ... private: int numPoints_; bool closed_polygon_; PolygonNode * first_; //指向多边形的第一个点 PolygonNode * last_; //指向多边形的最后一点}; // Polygon.cpp ... PolygonNode ::〜PolygonNode() { delete link_; //可能的问题区域 } Polygon ::〜Polygon() { delete first_; //可能的问题区域 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 :: operator =(const Polygon ply) { for(int i = 0; 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); //将最后一个指针指向新的最后一个点 last_ = ptr; if(first_ == NULL) first_ = last_; numPoints _ ++; } ... // main.cpp 多边形 ... Point pt0(0,0); Point pt1(1,1); ply.addPoint(pt0); cout<< ply =<层<< endl; Polygon newply; newply = ply; //使用赋值运算符 cout<< Polygon newply = ply; << endl; cout<< newply =<< newply< endl; cout<< ply =<层<< endl; newply.addPoint(pt1); cout<< newply.addPoint(Point(0,0));< endl; cout<< newply =<< newply< endl; cout<< ply =<层<< endl; ... 我在其他地方看过,这可能是由于OS 10.6或Xcode 3.2中的错误,如果有一个解决方法,有人可以给我详细的说明如何做解决方法,我没有很多Xcode的经验。 编辑:添加使用删除的部分代码 ,注意它正在Polygon和PolygonNode的析构函数中使用 编辑:添加了分配了link_的部分代码,setLink是一个简单的setter方法。 解决方案我看不到 PolygonNode 类的构造函数。在创建时是否将 link _ 指针初始化为null?如果没有,这可能是问题表现在你自己的错误。您必须确保 PolygonNode 实例中的链接_ 指针已初始化为null。定义适当的构造函数。 您是否为您的多边形类定义了一个复制构造函数?我不能看到一个在代码发布,但也许你只是没有粘贴它,你有一个。如果没有,这是严重问题的可能来源之一。 复制构造函数,由编译器自动合成只会复制 $ p 您的赋值运算符以值的形式接受参数 多边形& operator =(Polygon ply); 这使用了复制构造函数。如果是自动合成的,操作符中的 ply 具有指向同一列表的指针,参数通过值传递给操作符。 ply 的行为像它拥有的列表,当 ply 超出范围时,列表被销毁。 您还应该考虑一下这个问题:通过const引用在赋值运算符中取参数。我没有看到一个理由采取它的价值。也许你有一个,但即使你这样做,你可以暂时改变它,测试运算符,然后再定义正确的拷贝构造函数。在你的操作员,你应该检查自我分配。现在我可以看到,添加新的节点到旧的多边形。我不认为这是对的,但我想它只是为了测试。 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...The error is: malloc: * error for object 0x1001c0: pointer being freed was not allocated* set a breakpoint in malloc_error_break to debug.//Polygon.h// contains two classes PolygonNode and Polygonclass 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 classPolygonNode* 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 polygonPolygonNode* 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.cppPolygon 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.Edited: added parts of code that use delete, notice that it is being used in the destructors for Polygon and PolygonNodeEdited: added the part of the code where link_ is allocated, setLink is a simple setter method. 解决方案 I can't see the constructor for the PolygonNode class. 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 PolygonNode instances get initialized to 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.Your assignment operator takes the argument by valuePolygon& 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 behaves like it owned the list too and the list gets destroyed when ply goes out of scope. 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. 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. 这篇关于错误:释放的指针未分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-12 00:31