【发布时间】:2014-09-05 16:08:00
【问题描述】:
我有一个由这段代码引起的问题:
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';
}
}
和
char KernelFS::format(char 部分){
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个被阻塞,2个正在通过这段代码运行; 他们首先调用 mount,然后调用 format(每个都有自己的参数 Partition 对象,p1 和 p2)。 第一次调用 mount 时,它总是通过 - 然后在两个正在运行的线程中的任何一个调用 mount/format 期间随机出现断言失败。
通常,它在线程 1 期间失败 - 它调用 mount(..) 完成它,然后调用 format(...) 并失败: 删除文件证据[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);
我以前从未遇到过此错误,我不知道如何调试此错误,也不知道是什么原因造成的。 提前感谢您的帮助。
编辑: 当我删除标记的代码行(并忽略内存泄漏)时,没有断言失败。这是什么巫术?
! :)
【问题讨论】:
-
哪一行导致断言? assert 的信息是什么?
-
断言是在删除fileEvidention[...]后有注释的行引起的;在 format(...) 中,断言消息是:调试断言失败!路径.. 第 52 行.. 表达式:_BLOCK_TYPE_IS_VALID(pHead->nBlockUse)
标签: c++ multithreading concurrency assertion