根据 Visual C++ 运行时在析构函数中调用 free 时存在堆损坏。但我不明白为什么会出现堆损坏,谁能解释为什么?准确的错误是:
CRT detected that the application wrote to memory after end of heap buffer.
另外,如果我忽略错误,程序不会崩溃,它会继续运行,当我按下一个键时它会返回 0。
该类仅包含构造函数和析构函数以及私有变量FILE* target
和char* raw_data
.
foo::foo (wchar_t* path)
{
size_t size;
target = _wfopen (path, L"rb+");
if (!target) {
char* error = strerror (errno);
printf ("The file could not be opened: %s\n", error);
_exit (1);
}
fseek (target, 0L, SEEK_END);
size = ftell (target);
fseek (target, 0, SEEK_SET);
raw_data = (char*) malloc (size);
size = fread (raw_data, 1, size, target);
raw_data[size] = '\0';
}
foo::~foo ()
{
fclose (target);
free (raw_data);
}
int main ()
{
nbt* klas = new nbt (L"C:\\Users\\Ruben\\level");
puts ("Success?!");
delete klas;
getchar ();
return 0;
}