2014-09-05 114 views
-2

我具有由這個碼的一個問題:C++調試斷言失敗,使用WINDOWS.H互斥

char KernelFS::mount(Partition* part) { 
WaitForSingleObject(mutexFS,INFINITE); 
int pos; 
for(pos=0; pos<26; pos++) 
    if(mountedPartitions[pos] == 0) 
     break; 

if(pos < 26) { 

    mountedPartitions[pos] = part; 
    bitVectors[pos] = new BitVector(part); 
    fileEvidention[pos] = new ListHandler(); 

    openedFiles[pos] = 0; 
    forbidOpening[pos] = false; 

    ReleaseMutex(mutexFS); 
    return intToChar(pos); 
} 
else { 
    ReleaseMutex(mutexFS); 
    return '0'; 
} 

}

炭KernelFS ::格式(炭部分){

WaitForSingleObject(mutexFS,INFINITE); 
forbidOpening[charToInt(part)] = true; 
ReleaseMutex(mutexFS); 

while(openedFiles[charToInt(part)]>0) 
    WaitForSingleObject(unmountSem,INFINITE); 


WaitForSingleObject(mutexFS,INFINITE); 
// write fresh bit vector to cluster 0 of partition 
bitVectors[charToInt(part)]->formatBitVector(); 
openedFiles[charToInt(part)] = 0; 
forbidOpening[charToInt(part)] = false; 
delete fileEvidention; //!!***!! 
fileEvidention[charToInt(part)] = new ListHandler(); 

// some other stuff, irrelevant 

ReleaseMutex(mutexFS); 
return 1; 

}

有3個線程正在執行,1個被阻塞,兩個正在運行此代碼;他們首先調用mount,然後格式化(每個都有自己的參數Partition object,p1和p2)。 第一次調用mount時,它總是會經歷 - 然後在兩個正在運行的線程中的任何一個下一次調用mount/format時發生斷言失敗。它會調用mount(..)來完成它,然後調用格式(...)並且失敗: delete fileEvidention [charToInt(pos)]; (在調試模式下,當我達到這個指令,即使我嘗試去與F11,有一個斷言失敗)

如果它的事項...這是初始化:

char KernelFS::firstLetter = 'A';     // 'A' = 65 
Partition* KernelFS::mountedPartitions[26] = {0}; // init. no partitions are mounted 
BitVector* KernelFS::bitVectors[26] = {0};   // init. no partitions are mounted 
bool KernelFS::forbidOpening[26] = {false}; 
long KernelFS::openedFiles[26] = {0}; 
ListHandler* KernelFS::fileEvidention[26] = {0}; 

HANDLE KernelFS::mutexFS = CreateMutex(0,0,0); 
HANDLE KernelFS::unmountSem = CreateSemaphore(0,0,INFINITE,0); 

我從來沒有過這個錯誤,我不知道如何調試,也沒有什麼可能導致它。 感謝您的幫助,提前。

編輯: 當我刪除標記的代碼行(並忽略內存泄漏)沒有斷言失敗。這個巫術是什麼?

! :)

+0

哪一行導致斷言?什麼是斷言的信息? – Matt 2014-09-05 16:15:00

+0

斷言是在其後有註釋的行處引起的 delete fileEvidention [...]; 格式爲(...) 斷言信息爲: 調試斷言失敗! path .. line 52 .. 表達式:_BLOCK_TYPE_IS_VALID(pHead-> nBlockUse) – Alek988Alek 2014-09-05 16:21:28

回答

0

解決。應該是 刪除fileEvidention [charToInt(part)]; ......