int k =0;
string testimgurlofat;//全局变量的初始化
void Find(LPCTSTR lpszFileName)
{
CString strWildcard = lpszFileName;
strWildcard += _T("\*.dat");
CFileFind finder;
BOOL bFind = FALSE;
//查找文件
bFind = finder.FindFile(strWildcard);
while (bFind)
{ k=k+1;
TCHAR *buff;
testimgurlofat = (TCHAR *) malloc( Buff_size * sizeof (TCHAR));
bFind = finder.FindNextFile();
TCHAR argv[][13] = {"美","丽","赵","张","募","易"};
int m ;
for(m=0;m<6;m++){
TCHAR destinationkey[][10]= {"/l/","/s/","/q/","/z/","/m/","/y/"};
int n;
for(n=0;n<6;n++) {
memset( &testimgurlofat, 0, Buff_size ); //使用strncat最容易忘记 清空数组。
//获得找到文件的名称
CString strFileName = finder.GetFileName();
if(strstr(strFileName,argv[m]))
{ //testurlifat = (char *)realloc(testurlifat, strlen(testurlifat) + strlen(destinationkey[0] )+1);
testimgurlofat = destinationkey[n];
}
}
}
}
cout << testimgurlofat << endl;
//判断找到文件的是否包含"."或".."
if (finder.IsDots())
{
continue;
}
//获得找到文件的名称
if (finder.IsDirectory())
{
//找到文件的路径
CString strFilePath = finder.GetFilePath();
//递归查找文件
Find(strFilePath);
}
cout << k<< endl;
// free(testimgurlofat);
}
//结束查找
finder.Close();
}
两个多线程函数同时引用Find(lpszFileName)函数,运用两个相同值的“全局变量”组成新的字符串!
提示close.c这里的 (fh>=0&&(unsigned)fh <(unsigned)_nhandle)出错,已定位到两个引用同个“全局变量”的函数出错,因为我去掉这两个函数,程序没报错!
按Alt+7键查看Call Stack里面从上到下列出的对应从里层到外层的函数调用历史,查到close.c的报错提示,
int __cdecl _close (
int fh
)
{
int r; /* return value */
/* validate file handle */
_CHECK_FH_CLEAR_OSSERR_RETURN( fh, EBADF, -1 );
_VALIDATE_CLEAR_OSSERR_RETURN((fh >= 0 && (unsigned)fh < (unsigned)_nhandle), EBADF, -1);
_VALIDATE_CLEAR_OSSERR_RETURN((_osfile(fh) & FOPEN), EBADF, -1);
_lock_fh(fh); /* lock file */
__try {
if ( _osfile(fh) & FOPEN )
r = _close_nolock(fh);
else {
errno = EBADF;
r = -1;
_ASSERTE(("Invalid file descriptor. File possibly closed by a different thread",0));//怀疑两个多线程函数同时引用Find(lpszFileName)函数,文件可能被不同的线程关闭!
}
}
__finally {
_unlock_fh(fh); /* unlock the file */
}
return r;
}
程序中断在close.c的 _unlock_fh(fh); /* unlock the file */处。不知道怎么回事啊,调试了几天时间都没有解决,求各位大侠帮忙,江湖救急!在此谢过!