Note that there are some explanatory texts on larger screens.

plurals
  1. POSharing violation on file which really should be closed
    primarykey
    data
    text
    <p>I have an application that modifies an XML file by:</p> <p>(a) opening it,</p> <p>(b) creating a temporary file and writing a modified version to it,</p> <p>(c) closing both files, and</p> <p>(d) replacing the original file with the temporary file.</p> <p>When I test it on my laptop running Vista, everything works just as it should. On an embedded PC running XP Professional SP2 with a flash drive instead of a hard disk (which may or may not be relevant), it fails at step (d) with an access violation (error code 5). </p> <p>If I insert code between steps (c) and (d) to verify that the files are closed, it confirms that they are; if I put in code between steps (c) and (d) to try to delete the original file, it fails with a sharing violation (code 32). If I pause the program at this point and try to delete the file from the GUI, it fails with a sharing violation. If I use systinternals "Process Explorer" at this point, it shows the application still has a handle to the file.</p> <p>Here is some of the code:</p> <pre><code>// Open the file which is to be updated: _wfopen_s(&amp;inStream, m_fileName, L"r, ccs=UTF-8"); // Obtain a suitable temporary filename from the operating system: TCHAR lpTempPathBuffer[MAX_PATH]; // Buffer to hold temporary file path TCHAR szTempFileName[MAX_PATH]; // Buffer to hold temporary file name GetTempPath(MAX_PATH, lpTempPathBuffer); GetTempFileName(lpTempPathBuffer, TEXT("TMP"), 0, szTempFileName); // Now open a temporary file to hold the updates: errno_t err = _wfopen_s(&amp;outStream, szTempFileName, L"w, ccs=UTF-8"); if (err == 0) printf ("Temporary file opened successfully\r\n"); else printf ("Temporary file not opened; error code %d\r\n", err); </code></pre> <p>Then the gubbins that modifies the file, and then ...</p> <pre><code>// Finally, we must close both files and copy the temporary file to // overwrite the original input file: int closerr = fclose(inStream); if (closerr == 0) printf("Original file closed properly\r\n"); else printf("Original file not closed properly\r\n"); closerr = fclose(outStream); if (closerr == 0) printf("Temp file closed properly\r\n"); else printf("Temp file not closed properly\r\n"); int numclosed = _fcloseall(); printf("Number of files closed = %d\r\n", numclosed); // Should be zero, as we've already closed everything manually if (!DeleteFile(m_fileName)) { int err = GetLastError(); printf ("Delete file failed, error code was %d\r\n", err); } else printf ("Delete file succeeded\r\n"); if (!MoveFileEx(szTempFileName, m_fileName, MOVEFILE_REPLACE_EXISTING | MOVEFILE_COPY_ALLOWED | MOVEFILE_WRITE_THROUGH)) { int err = GetLastError(); printf ("Move file failed, error code was %d\r\n", err); } else printf ("Move file succeeded\r\n"); </code></pre> <p>The output log shows:</p> <p>"Temporary file opened successfully</p> <p>Original file closed properly</p> <p>Temp file closed properly</p> <p>Number of files closed = 0</p> <p>Delete file failed, error code was 32</p> <p>Move file failed, error code was 5"</p> <p>This makes no sense... Why am I getting a sharing violation on a file which the operating system insists is closed? And is there a reason why this works in Vista but not in XP?</p> <p>Many thanks for any advice, Stephen.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload