尝试释放内存时出现 CrtIsValidHeapPointer 错误 [C++]
Getting CrtIsValidHeapPointer Error when trying to release memory [C++]
我有一个练习,我需要创建一个引擎类和一个汽车类。
这些是我代码的一部分:
车.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
函数中,您还应该释放已分配的内存,这样就不会有内存泄漏。
相关文章:
- 警告处理为错误这里有什么问题
- "error: no matching function for call to"构造函数错误
- boost::进程间消息队列引发错误
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- QT在错误的班级中寻找空位
- vector.resize()中的分配错误
- 代码在main()中运行,但在函数中出现错误
- 释放错误后堆使用
- (C++)分析树以计算返回错误值的简单算术表达式
- Project Euler问题4的错误解决方案
- 我的字符计数代码计算错误.为什么
- 从"int*"强制转换为"unsigned int"会丢失精度错误
- 尝试导入pybind-opencv模块时出现libgtk错误
- CMake项目Boost库错误:Boost/config/compiler/gcc.hpp:165:10:致命错误:cs
- 在某些循环内使用vector.push_back时出现分段错误
- MSVC多行宏编译器错误
- 静态数据成员的问题-修复链接错误会导致编译器错误
- 为什么在运行时没有向我们提供有关分段错误的更多信息?
- 错误:未在此范围内声明'reverse'