问题描述
当在Tree的析构函数中加上delete type_的时候就会报如上错误,不知道时什么原因?
把delete type_注释后不会报错
用代码块功能插入代码,请勿粘贴截图
#include<iostream>
#include<map>
#include<mutex>
#include<vector>
#include<string>
using namespace std;
class TreeType {
public:
string name_;
string color_;
string texture_;
public:
TreeType():name_("a"),color_("b"),texture_("c") {}
TreeType(const string &name, const string color, const string texture) :name_(name), color_(color), texture_(texture)
{
//...构造
}
~TreeType() { cout << "Type_destruct " << this << endl; }
//对象行为
void draw(const string canvas, double x, double y) {
cout << "canvas: "<< name_ << " " << color_ << " " << x << " " << y << endl;
}
};
class Tree {
public:
Tree(double x, double y, TreeType* tp) :x_(x), y_(y) {
type_ = new TreeType();
if (nullptr != tp) {
memcpy(type_, tp, sizeof(*tp));
}
cout << "Create Tree " << x_ << " " << y_ << " " << type_->name_<<" ";
cout << "Tree_id=" << this << " Tree_type="<<type_<<endl;
cout << "----------" << endl;
}
~Tree() {
if (nullptr != type_) {
cout << "delete Tree" << x_ << " " << y_ << " " << type_->name_<<" ";
cout << "Tree_id=" << this << " tree_type = "<<type_<<endl;
delete type_;
cout << "----------" << endl;
}
}
Tree(const Tree& tree) {
cout << "copy_cstrt " << &tree <<" tree_type " <<tree.type_<< endl;
if (&tree == this) {
}
if ((&tree) != nullptr) {
x_ = tree.x_;
y_ = tree.y_;
type_ = new TreeType();
memcpy(type_, tree.type_, sizeof(*tree.type_));
cout << this->type_->name_ << endl;
}
}
Tree& operator=(const Tree& tree) {
if (&tree == this) {
return *this;
}
else if (nullptr != &tree) {
x_ = tree.x_;
y_ = tree.y_;
if (nullptr != type_) {
delete type_;
}
type_ = new TreeType();
memcpy(type_, tree.type_, sizeof(*tree.type_));
}
}
void draw(const string canvas) {
type_->draw(canvas, x_, y_);
}
public:
double x_;
double y_;
TreeType* type_;
};
class TreeFactory {
public:
static TreeFactory* getinstance() {
if (instance_ == nullptr) {
mutex_.lock();
if (instance_ == nullptr) {
instance_ = new TreeFactory();
}
mutex_.unlock();
}
return instance_;
}
TreeType* getTreetype(const string name, const string color, const string texture) {
std::string key = name + "_" + color + "_" + texture;
map<string, TreeType*>::iterator it = tree_types_.find(key);
if (it == tree_types_.end()) {
TreeType* new_type = new TreeType(name, color, texture);
tree_types_[key] = new_type;
cout << "get_treetype: " << new_type << endl;
return new_type;
}
else {
return it->second;
}
}
public:
TreeFactory() {}
static TreeFactory* instance_;
static std::mutex mutex_;
map<std::string, TreeType*> tree_types_;
};
TreeFactory* TreeFactory::instance_ = nullptr;
std::mutex TreeFactory::mutex_;
class Forest {
public:
void plantree(double x, double y, const string name, const string color, const string conture) {
TreeType* type = TreeFactory::getinstance()->getTreetype(name, color, conture);
Tree tree(x, y, type);
trees_.push_back(tree);
}
void draw() {
for (auto& tree : trees_) {
tree.draw("canvas");
}
}
public:
std::vector<Tree> trees_;
};
int main() {
Forest* forest = new Forest();
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
double x = i;
double y = j;
forest->plantree(x, y, "杉树", "红色", "");
forest->plantree(x, y, "榕树", "绿色", "");
forest->plantree(x, y, "桦树", "白色", "");
}
}
forest->draw();
delete forest;
return 0;
}
运行结果及报错内容
我的解答思路和尝试过的方法
把delete type_注释掉可以正常运,但是type_是动态分配的,析构函数不就该delete以防止内存泄露吗?