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

时间:2016-03-12 14:21:12

标签: c++ visual-studio memory

我有一个练习,我需要创建一个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;
}

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

Engine.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;

};

Exc.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,然后我得到&#34; CrtIsValidHeapPointer&#34;。

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

THX。

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;

1 个答案:

答案 0 :(得分:1)

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

然后在主程序中执行

car.SetEngine(&engine);

从而将_engine更改为指向您尚未new动态分配的对象,并丢失原始指针并给您内存泄漏。< / p>

因此,在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功能中,您还应该释放已经分配的内存,这样就不会有内存泄漏。