zhang_xiaoqiang 2022-11-26 14:14
浏览 6
已结题

VS报错:0xC0000005: 写入位置 0xD时发生访问冲突。

问题描述

img


当在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;
}
运行结果及报错内容

img

我的解答思路和尝试过的方法

把delete type_注释掉可以正常运,但是type_是动态分配的,析构函数不就该delete以防止内存泄露吗?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    问题事件

    • 系统已结题 12月4日
    • 创建了问题 11月26日

    悬赏问题

    • ¥15 需要手写数字信号处理Dsp三个简单题 不用太复杂
    • ¥15 数字信号处理考试111
    • ¥100 关于#audobe audition#的问题,如何解决?
    • ¥15 allegro17.2生成bom表是空白的
    • ¥15 请问一下怎么打通CAN通讯
    • ¥20 如何在 rocky9.4 部署 CDH6.3.2?
    • ¥35 navicat将excel中的数据导入mysql出错
    • ¥15 rt-thread线程切换的问题
    • ¥15 高通uboot 打印ubi init err 22
    • ¥15 R语言中lasso回归报错