繁体   English   中英

尝试释放内存时出现CrtIsValidHeapPointer错误[C ++]

[英]Getting CrtIsValidHeapPointer Error when trying to release memory [C++]

我有一个练习,需要创建一个Engine类和一个Car类。

这些是我的代码的一部分:

Car.cpp

Car::Car()
{
    _engine = new Engine();
    _manufacturer = new char[10];
    _prod_year = 0;
    _color = new char[10];
}

Car::~Car()
{ 
    // Releasing allocated memory
    delete[] _manufacturer;
    delete[] _color;
    delete _engine;
}

void Car::print(void) const
{
    (*_engine).print();
    cout << "\n" << endl;
    cout << "Manufacturer:" << _manufacturer << "\n" << endl;
    cout << "Production Year:" << _prod_year << "\n" << endl;
    cout << "Color:" << _color << endl;
}

汽车

class Car
{

    Engine * _engine;
    char * _manufacturer;
    int _prod_year;
    char * _color;

    public:

    /*
    * Creates a Car with the default set of attributes.
    */

    Car();
    virtual ~Car();
    inline Engine * GetEngine() { return _engine; }
    void SetEngine(Engine * engine) { _engine = engine; }
    inline char * GetManufacturer(){ return _manufacturer; }
    void SetManufacturer(char * manufacturer){_manufacturer = manufacturer;}
    inline int GetProdYear() { return _prod_year; }
    void SetProdYear(int prod_year) { _prod_year = prod_year; }
    inline char * GetColor() { return _color; }
    void SetColor(char * color) { _color = color; }
    void print(void) const;
};

引擎

class Engine
{
    /*  
        Represents an Engine with the following attributes:
        * int Hourse Power
        * char * Manufacturer
        * int Production Year
    */

    int _horse_power;
    char * _manufacturer;
    int _prod_year;

public:
    Engine();
    virtual ~Engine();
    int GetHorsePower() { return _horse_power; }
    void SetHorsePower(int horse_power) { _horse_power = horse_power; }
    char * GetManufacturer(){ return _manufacturer; }
    void SetManufacturer(char * manufacturer){_manufacturer = manufacturer;}
    int GetProdYear() { return _prod_year; }
    void SetProdYear(int prod_year) { _prod_year = prod_year; }
    void print() const;

};

执行文件

Engine engine;
engine.SetHorsePower(150);
cout << "Current Horse Power: " <<engine.GetHorsePower()<<endl;

char * manufacturer = new char[strlen("VP") +1];
strcpy(manufacturer, "VP");
engine.SetManufacturer(manufacturer);
cout << "Current Manufacturer: " << engine.GetManufacturer() << endl;

engine.SetProdYear(1995);
cout << "Current Production Year: " << engine.GetProdYear() << endl;

cout << "\n ------------- Printing engine details -------------\n" << endl;
engine.print();
Car car;
car.SetEngine(&engine);
cout << "Current Engine: " << endl;
(*car.GetEngine()).print();

char * car_manufacturer = new char[strlen("Toyota") + 1];
car_manufacturer = { "Toyota" };
car.SetManufacturer(car_manufacturer);
cout << "Current Car Manufacturer: " << car.GetManufacturer() << endl;

car.SetProdYear(2010);
cout << "Current Car Production Year: " << car.GetProdYear() << endl;

char * color = new char[strlen("Yellow") + 1];
color = { "Yellow" };
car.SetColor(color);
cout << "Current Car Color: " << car.GetColor() << endl;

cout << "\n ------------- Printing car details -------------\n" << endl;

car.print();

我的程序运行正常,直到到达〜Car,然后得到“ CrtIsValidHeapPointer”。

有人可以告诉我我做错了什么吗?

谢谢。


Engine * engine = new Engine;
engine->SetHorsePower(150);
cout << "Current Horse Power: " << engine->GetHorsePower()<<endl;

char * manufacturer = new char[strlen("VP") +1];
strcpy(manufacturer, "VP");
engine->SetManufacturer(manufacturer);
cout << "Current Manufacturer: " << engine->GetManufacturer() << endl;

engine->SetProdYear(1995);
cout << "Current Production Year: " << engine->GetProdYear() << endl;

cout << "\n ------------- Printing engine details -------------\n" << endl;
engine->print();

Car car;
car.SetEngine(engine);
cout << "Current Engine: " << endl;
car.GetEngine()->print();

char * car_manufacturer = new char[strlen("Toyota") + 1];
strcpy(car_manufacturer, "Toyota");
car.SetManufacturer(car_manufacturer);
cout << "Current Car Manufacturer: " << car.GetManufacturer() << endl;

car.SetProdYear(2010);
cout << "Current Car Production Year: " << car.GetProdYear() << endl;

char * color = new char[strlen("Yellow") + 1];
strcpy(color, "Yellow");
car.SetColor(color);
cout << "Current Car Color: " << car.GetColor() << endl;

cout << "\n ------------- Printing car details -------------\n" << endl;

car.print();

return 0;

Car构造函数中,您动态分配Engine并分配给_engine

然后在您的主程序中

car.SetEngine(&engine);

从而将_engine更改为指向您尚未使用new动态分配的对象,并丢失了原始指针并给您带来了内存泄漏。

因此,在Car析构函数中,您尝试删除未分配的指针,该指针没有导致new 未定义行为

字符串也有问题,您在执行类似

char * car_manufacturer = new char[strlen("Toyota") + 1];
car_manufacturer = { "Toyota" };

首先分配内存,然后将指向该内存的指针分配给car_manufacturer 然后,紧接着,您将car_manufacturer指向其他位置,再次丢失指向已分配内存的指针,并发生新的内存泄漏。 除此之外,然后将car对象中的指针设置为字符串文字"Toyota" ,该指针再次指向未使用new[]分配的内存,因此再次执行delete[]会导致未定义的行为


即使您不想使用std::string (这是推荐的解决方案),使用字符串解决问题也很容易,这就是复制字符串而不是重新分配指针:

char * car_manufacturer = new char[strlen("Toyota") + 1];
strcpy(car_manufacturer, "Toyota");

为了解决引擎的第一个问题,如果必须调用SetEngine ,则还需要动态分配新引擎。

Set函数中,您还应该释放已经分配的内存,这样就不会发生内存泄漏。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM