1

出于某种原因,我从以下类定义文件中得到了一个奇怪的堆损坏,很可能是在“CreateFromNode”函数中。谁能帮我这个?自从我抓住它以来,我真的很想立即处理它,并且以后不会看到它出现。此外,没有外部力量正在修改此类的任何成员(或其成员)。

#include "Object.h"

TmxMap::Object::Object():name(), position(), size(), gid(-1), visible(true),
properties(), image(), polygon(NULL), polyline(NULL), ellipse(false){}

TmxMap::Object::Object(rapidxml::xml_node<>* const& object_node):name(),
    position(), size(), gid(-1), visible(true), properties(), image(),
    polygon(NULL), polyline(NULL), ellipse(false){
CreateFromNode(object_node);
}

TmxMap::Object::Object(TmxMap::Object const& from):name(from.name),
    position(from.position), size(from.size), gid(from.gid),
    visible(from.visible), properties(from.properties),
    image(from.image), polygon(from.polygon), polyline(from.polyline),
    ellipse(from.ellipse){}

TmxMap::Object::~Object(){
DeleteObjects();
}

void TmxMap::Object::CreateFromNode(rapidxml::xml_node<>* const& object_node){
DeleteObjects();

rapidxml::xml_node<>* data_node = NULL;
rapidxml::xml_attribute<>* data_attrib = NULL;

if((data_attrib = object_node -> first_attribute("name")))
    name = data_attrib -> value();

if((data_attrib = object_node -> first_attribute("width")))
    size.x = std::strtol(data_attrib -> value(), NULL, 10);
if((data_attrib = object_node -> first_attribute("height")))
    size.y = std::strtol(data_attrib -> value(), NULL, 10);

position.Set(
        std::strtol(object_node -> first_attribute("x") -> value(), NULL, 10),
        std::strtol(object_node -> first_attribute("y") -> value(), NULL, 10)
);

if((data_node = object_node -> first_node("polygon")))
    polygon = new Polygon2D<int>(data_node);

if((data_node = object_node -> first_node("polyline")))
    polyline = new Polyline2D<int>(data_node);

ellipse = (object_node -> first_node("ellipse") != NULL);
}

void TmxMap::Object::DeleteObjects(){
if(polygon){delete polygon; polygon = NULL;}
if(polyline){delete polyline; polyline = NULL;}
}

void TmxMap::Object::Destroy(){
name.clear();
position.Set();
size.Set();
gid = 0;
visible = true;
properties.Destroy();
image.Destroy();
DeleteObjects();
ellipse = false;
   }
4

1 回答 1

1

您的复制构造函数复制指针polygonpolyline.
这将导致它们被多次删除。

您应该复制这些对象而不是指针。

于 2013-03-04T08:30:18.227 回答