我正在使用VTK與MSVC並嘗試加載數據時得到一個奇怪的行爲。我修改了一下它,甚至下面的代碼會產生一個堆損壞,任何想法正在發生什麼或可能出錯?使用strdup堆損壞
vtkAbstractArray *vtkDataReader::ReadArray(const char *dataType, int numTuples, int numComp)
{
char* type=strdup(dataType);
free(type); // <--- here the heap corrution appears
...
這是調用堆棧:使用此代碼代替的strdup工作得很好,是的strdup莫名其妙上破MSVC
:
> 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++
編輯?
char *type=(char*)malloc(100);
strcpy(type,dataType);
可能需要一些更多的上下文這裏,你這是在傳遞作爲的dataType?你在「strdup」和「free」調用之間做什麼操作? – slugonamission 2012-01-05 10:02:54
你在'strdup'和'free'之間做什麼?你可能寫在超出重複字符串末尾的地方。 – 2012-01-05 10:04:04
dataType是內容爲「float」的char,在strdup和free之間沒有操作。 – Dirk 2012-01-05 10:05:14