今天写demo代码的时候,忽然free释放时候的报错。错误内容大致是:
HEAP CORRUPTION DETECTED: after Normal block (#577) at 0x02F01BA0. CRT detected that the application wrote to memory after end of heap buffer.
示例代码大概是这样
代码语言:javascript复制wchar_t* ptr = (wchar_t*)malloc(8*sizeof(wchar_t));
wcscpy(ptr,L"12345678");
....
free(ptr);
然后free时就报错,忽略错误的话还能正常运行。这个原因也很简单,在wscpy的时候,写入内存东西超出了内存大小。解决起来很简单,多申请一个size的就行了,比如改为wchar_t* ptr = (wchar_t*)malloc((8 1)*sizeof(wchar_t));
以前没注意过这个问题,特此记录下。