代码之家  ›  专栏  ›  技术社区  ›  Jordan

错误:未分配要释放的指针

  •  2
  • Jordan  · 技术社区  · 16 年前

    我试图重载赋值操作符来做一个多边形对象的深度复制,程序会编译,但是我在最后得到一个错误,我想澄清。下面是相关代码,如果你认为我需要添加更多请发表评论。假设适当的 #include 这就是 <<

    错误是:malloc: 在malloc\u error\u 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;
    

    ...

    已编辑:添加了使用 delete

    编辑:添加了分配链接的代码部分,setLink是一个简单的setter方法。

    3 回复  |  直到 16 年前
        1
  •  2
  •   Maciej Hehl    16 年前

    我看不到 PolygonNode link_ 指针在创建时初始化为空?如果不是的话,那可能就是问题在你得到的错误中显现出来了。你必须确保 链接_ 中的指针 多边形节点 实例被初始化为null。定义适当的构造函数。

    由编译器自动合成的复制构造函数将只复制 Polygon

    Polygon& operator= (Polygon ply);
    

    这将使用复制构造函数。如果是自动合成的, ply 使用 它的行为就像它也拥有这个列表,当

    您还应该考虑通过const引用获取赋值运算符中的参数。我看不出有什么理由按价值来考虑。也许你有一个,但即使你有,你可以暂时改变它,测试操作符,在你定义正确的复制构造函数之前。在操作符中,您应该检查自我分配。我现在所能看到的就是在旧节点上添加新节点 多边形 . 我不认为这是对的,但我想现在只是为了测试。

        2
  •  0
  •   Sadeq    16 年前

    link_ 变量,它在您的示例中没有分配,也从未使用过。。。

        3
  •  -1
  •   Puppy    16 年前

    通常,除非在专用类中,否则永远不应该使用原始指针。将它们更改为智能指针(在这种情况下,自动或共享即可),并且不再需要释放自己的内存—问题已解决。 编辑:

    更聪明的选择是只使用std::list或std::vector。