when compiling the application I get

"Unhandled exception at 0x57cb47af (msvcr100d.dll) in Os_pro.exe: 0xC0000005: Access violation reading location 0x00000000."

What does this mean and how to treat it?

Code (part of the code gets format files and initial paths of hard disks as parameters)

enter code here bool FindByMask(int param) { int i = param+1; printf("Thread number %i start.\n", i); for(int i = 0; i < maskCount; ) { _TCHAR *drive = drives[param]; _TCHAR *mask = new _TCHAR[_tcslen(maska[i])]; _tcscpy(mask, _T("*.")); _tcscat(mask, maska[i]); find(drive, mask); ++i; } return true; } bool find(_TCHAR *drive, _TCHAR *mask) { _TCHAR tmp1[MAX_PATH]; _TCHAR *tmp2; WIN32_FIND_DATA findData; HANDLE hFileFind; memset(&findData, 0, sizeof(WIN32_FIND_DATA)); fstream fstr; _tcscpy(tmp1, drive); _tcscat(tmp1, _T("*.*")); hFileFind = FindFirstFile(tmp1, &findData); do { if(hFileFind != INVALID_HANDLE_VALUE) { if ((_tcscmp(findData.cFileName, _T(".")) != 0) && (_tcscmp(findData.cFileName, _T("..")) != 0)) { if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { _TCHAR newpath[MAX_PATH]; tmp2 = _tcssubstr(tmp1, 0, _tcslen(tmp1)-4); _tcscpy(newpath, tmp2); _tcscat(newpath, findData.cFileName); _tcscat(newpath, _T("\\")); find(newpath, mask); } } } } while(FindNextFile(hFileFind, &findData)); FindClose(hFileFind); _TCHAR *findPath = new _TCHAR[_tcslen(drive)+_tcslen(mask)]; _tcscpy(findPath, drive); _tcscat(findPath, mask); hFileFind = FindFirstFile(findPath, &findData); if(hFileFind != INVALID_HANDLE_VALUE) { do { if(!(findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) { tmp2 = _tcssubstr(tmp1, 0, _tcslen(tmp1)-4); _TCHAR *fp = new _TCHAR[_tcslen(tmp2) + _tcslen(findData.cFileName)]; _tcscpy(fp, tmp2); _tcscat(fp, findData.cFileName); _TCHAR *tmp3 = new _TCHAR[_tcslen(fp)+2]; _tcscpy(tmp3, _T("\"")); _tcscat(tmp3, fp); _tcscat(tmp3, _T("\"")); DWORD dwAttr = GetFileAttributes(fp); if(dwAttr & FILE_ATTRIBUTE_ARCHIVE) { FILE* f = fopen(".\\list_file.lst", "a+"); fwprintf_s(f, _T("%s"), tmp3); fwprintf_s(f, _T("%s"), _T("\n")); SetFileAttributes(fp, dwAttr & FILE_ATTRIBUTE_ARCHIVE); fclose(f); _TCHAR *cml; cml = new _TCHAR[_tcslen(tmp_1)+_tcslen(tmp3)+_tcslen(tmp_3)]; _tcscpy(cml, tmp_1); _tcscat(cml, tmp_3); _tcscat(cml, _T(" ")); _tcscat(cml, tmp3); STARTUPINFO siStartupInfo; PROCESS_INFORMATION piProcessInfo; memset(&siStartupInfo, 0, sizeof(siStartupInfo)); memset(&piProcessInfo, 0, sizeof(piProcessInfo)); siStartupInfo.cb = sizeof(siStartupInfo); CreateProcess(_T(".\\rar.exe"), cml, 0, 0, FALSE, NORMAL_PRIORITY_CLASS, 0, _T(".\\"), &siStartupInfo, &piProcessInfo); WaitForSingleObject( piProcessInfo.hProcess, INFINITE); } } } while(FindNextFile(hFileFind, &findData)); } FindClose(hFileFind); return true; }`enter code here 
  • This is probably not when compiling, but when executing a program. Error reading memory where you can not read. It would be necessary to look at the code for the diagnosis - yapycoder
  • code laid out ... - Daniel Pavlov
  • Here would be another line of code with an error to find out. Do you have an error in debugging? Here it is not clear where the variable drives come from, for example. Maybe on this line crash _TCHAR *drive = drives[param]; - yapycoder
  • error in: cml = new _TCHAR [_tcslen (tmp_1) + _ tcslen (tmp3) + _ tcslen (tmp_3)]; - Daniel Pavlov

1 answer 1

Maybe this line is crashing

 _TCHAR *drive = drives[param]; 

In this fragment, for example, you do not have enough space for mask, you need 2 more characters.

 _TCHAR *mask = new _TCHAR[_tcslen(maska[i])]; _tcscpy(mask, _T("*.")); _tcscat(mask, maska[i]); 

There is no explicit check that there is enough space allocated for tmp1 :

 _tcscpy(tmp1, drive); _tcscat(tmp1, _T("*.*")); 


and where is the memory for tmp_1 allocated and where is it initialized? It may be an error when trying to calculate the length of a string when searching for a terminating zero, the length function goes far beyond the allocated memory.

Update: the same goes for tmp_3

  • error in: cml = new _TCHAR [_tcslen (tmp_1) + _ tcslen (tmp3) + _ tcslen (tmp_3)]; a - Daniel Pavlov
  • updated answer. nothing is known about tmp_1 and tmp_3 - yapycoder
  • when allocating memory, the constant must be specified as the size of the allocated memory, and here the dynamics are obtained; the compiler always has to calculate. in C ++ 0x, if you know for sure that functions will be returned to a constant, we specify constexpr, then there will be no problems. - rojaster