尝试删除对象时检测到堆损坏 (C++)
Heap Corruption Detected when trying to delete an object (C++)
我在程序结束时删除对象时遇到问题。这是针对 C++ 课程的,因此我们不允许使用字符串 class(目前)。我有一个 Weapon class 为武器生成一个名称,这个名称用 char* result = new char[len]
实例化,然后返回给构造函数。在析构函数中,我使用 delete[] this->name
.
删除名称对象
问题:
当我运行我的程序时,一切运行都很好,直到程序进入程序的删除部分。然后我收到此错误消息:
Debug Error!
Program: ... path to program ...
HEAP CORRUPTION DETECTED: after Normal block (#198) at 0x0100B918. CRT
detected that the application wrote to memory after end of heap
buffer.
(Press Retry to debug the application)
我试过用 delete[] 替换 delete,反之亦然,但没有区别。
谁能看出我哪里错了?
main.cpp:
int _tmain(int argc, _TCHAR* argv[]) {
// ... code ...
Weapon* weapon = new Weapon();
// ... code ...
delete weapon;
}
Weapon.cpp:
Weapon::Weapon() {
this->name = this->generateName();
// more properties...
}
Weapon::~Weapon() {
delete[] this->name;
this->name = nullptr;
}
char* Weapon::generateName() {
int pr, tp, su; // random variables for picking prefix, type and suffix
const char *prefix[10] = { // ... a bunch of names ... };
const char *type[10] = { // ... a bunch of names ... };
const char *suffix[10] = { // ... a bunch of names ... };
pr = rand() % 9;
tp = rand() % 9;
su = rand() % 9;
int len = strlen(prefix[pr]) + strlen(type[tp]) + strlen(suffix[su]) + 1;
char *result = new char[len]();
strcpy(result, prefix[pr]);
strcat(result, " ");
strcat(result, type[tp]);
strcat(result, " ");
strcat(result, suffix[su]);
return result;
}
您忘记在字符串中为空格分配空间 :
int len = strlen(prefix[pr]) + strlen(type[tp]) + strlen(suffix[su]) + 1;
应该是:
int len = strlen(prefix[pr]) + 1 + strlen(type[tp]) + 1 + strlen(suffix[su]) + 1;
这两个额外的字符会覆盖分配块之外的内存,这就是检测到的堆损坏的原因。
我在程序结束时删除对象时遇到问题。这是针对 C++ 课程的,因此我们不允许使用字符串 class(目前)。我有一个 Weapon class 为武器生成一个名称,这个名称用 char* result = new char[len]
实例化,然后返回给构造函数。在析构函数中,我使用 delete[] this->name
.
问题:
当我运行我的程序时,一切运行都很好,直到程序进入程序的删除部分。然后我收到此错误消息:
Debug Error!
Program: ... path to program ...
HEAP CORRUPTION DETECTED: after Normal block (#198) at 0x0100B918. CRT detected that the application wrote to memory after end of heap buffer.
(Press Retry to debug the application)
我试过用 delete[] 替换 delete,反之亦然,但没有区别。
谁能看出我哪里错了?
main.cpp:
int _tmain(int argc, _TCHAR* argv[]) {
// ... code ...
Weapon* weapon = new Weapon();
// ... code ...
delete weapon;
}
Weapon.cpp:
Weapon::Weapon() {
this->name = this->generateName();
// more properties...
}
Weapon::~Weapon() {
delete[] this->name;
this->name = nullptr;
}
char* Weapon::generateName() {
int pr, tp, su; // random variables for picking prefix, type and suffix
const char *prefix[10] = { // ... a bunch of names ... };
const char *type[10] = { // ... a bunch of names ... };
const char *suffix[10] = { // ... a bunch of names ... };
pr = rand() % 9;
tp = rand() % 9;
su = rand() % 9;
int len = strlen(prefix[pr]) + strlen(type[tp]) + strlen(suffix[su]) + 1;
char *result = new char[len]();
strcpy(result, prefix[pr]);
strcat(result, " ");
strcat(result, type[tp]);
strcat(result, " ");
strcat(result, suffix[su]);
return result;
}
您忘记在字符串中为空格分配空间 :
int len = strlen(prefix[pr]) + strlen(type[tp]) + strlen(suffix[su]) + 1;
应该是:
int len = strlen(prefix[pr]) + 1 + strlen(type[tp]) + 1 + strlen(suffix[su]) + 1;
这两个额外的字符会覆盖分配块之外的内存,这就是检测到的堆损坏的原因。