问题描述
我将 VTK 与 MSVC 一起使用,并在尝试加载数据时出现奇怪的行为.我对它进行了一些修改,即使是以下代码也会产生堆损坏,您知道发生了什么或可能出了什么问题吗?
I am using VTK together with MSVC and get a strange behaviour when trying to load data. I tinkered a little bit with it and even the following code generates a heap corruption, any ideas what is happening or what possibly went wrong?
vtkAbstractArray *vtkDataReader::ReadArray(const char *dataType, int numTuples, int numComp)
{
char* type=strdup(dataType);
free(type); // <--- here the heap corrution appears
...
这是调用堆栈:
> msvcr90d.dll!_CrtIsValidHeapPointer(const void * pUserData=0x00691da0) Zeile 2103 C++
msvcr90d.dll!_free_dbg_nolock(void * pUserData=0x00691da0, int nBlockUse=1) Zeile 1317 + 0x9 Bytes C++
msvcr90d.dll!_free_dbg(void * pUserData=0x00691da0, int nBlockUse=1) Zeile 1258 + 0xd Bytes C++
msvcr90d.dll!free(void * pUserData=0x00691da0) Zeile 49 + 0xb Bytes C++
Simulator.exe!vtkDataReader::ReadArray(const char * dataType=0x0370b734, int numTuples=24576, int numComp=3) Zeile 1401 + 0xc Bytes C++
Simulator.exe!vtkDataReader::ReadPoints(vtkPointSet * ps=0x081702d0, int numPts=24576) Zeile 1936 + 0x15 Bytes C++
使用此代码代替 strdup 效果很好,strdup 是否在 msvc 上以某种方式损坏?
using this code instead of strdup works nicely, is strdup somehow broken on msvc?
char *type=(char*)malloc(100);
strcpy(type,dataType);
推荐答案
strdup 本身在 msvc 中已被弃用,并且网上有类似堆损坏的报告,微软表示您应该改用 _strdup
strdup as such is deprecated in msvc and there are reports of similar heap corruption around the web, microsoft states you should use _strdup instead
http://msdn.microsoft.com/en-us/library/ms235454
这篇关于使用 strdup 进行堆损坏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!