[英]error: pointer being freed was not allocated
我試圖使賦值運算符重載,以對多邊形對象進行深層復制,程序可以編譯,但是在最后要清除的地方出現錯誤。 以下是相關代碼,如果您認為我需要添加更多內容,請發表評論。 假設正確的#include
且<<
操作符已重載以實現正確的輸出等...
錯誤是: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;
...
我在其他地方讀過,這可能是由於OS 10.6或Xcode 3.2中的錯誤引起的,如果有解決方法,有人可以給我詳細的說明,以解決該問題,但我對Xcode經驗不足。
編輯:添加了使用delete
的代碼部分,請注意,它在Polygon和PolygonNode的析構函數中使用
編輯:添加了分配link_的部分代碼,setLink是一個簡單的setter方法。
我看不到PolygonNode
類的構造函數。 創建時link_
指針是否初始化為null? 如果不是這樣,那可能是在您得到的錯誤中表現出來的問題。 您必須確保將PolygonNode
實例中的link_
指針初始化為null。 定義適當的構造函數。
您是否為多邊形類定義了復制構造函數? 我在發布的代碼中看不到一個,但也許您只是沒有粘貼而已。 如果不是,那可能是嚴重問題的根源之一。
編譯器自動合成的復制構造函數將只復制Polygon
類中的指針。
您的賦值運算符按值接受參數
Polygon& operator= (Polygon ply);
這利用了復制構造函數。 如果是自動合成的,則操作符內部的ply
將具有指向同一列表的指針,按值傳遞給操作符的參數將擁有。 ply
行為也像它擁有該列表一樣,並且當ply
超出范圍時,該列表將被銷毀。 原始參數保留有懸空指針。
您應該定義正確的副本構造函數。
您還應該考慮通過const引用在賦值運算符中使用參數。 我認為沒有理由重視它。 也許您有一個,但是即使您有一個,也可以在定義正確的副本構造函數之前臨時更改它以測試操作符。 在您的操作員中,您應檢查自我分配。 我現在看到的就是在舊的Polygon
添加新節點。 我認為這是不對的,但我想現在僅用於測試。
我認為問題是link_
變量,它在您的示例中沒有分配,並且從未使用過...
通常,除非在專用類中,否則永遠不要使用原始指針。 將它們更改為智能指針(在這種情況下將使用自動或共享指針),然后不再需要釋放自己的內存->問題已解決。 編輯:
聰明的選擇是只使用std :: list或std :: vector。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.