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

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

本文关键字:错误 CrtIsValidHeapPointer C++ 释放 内存      更新时间:2023-10-16

我有一个练习,我需要创建一个引擎类和一个汽车类。

这些是我代码的一部分:

车.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;
}

车.h

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;
};

发动机.h

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;
};

除.cpp

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函数中,您还应该释放已分配的内存,这样就不会有内存泄漏。