المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : Weird threading error with LibTiff



C++ Programming
10-02-2010, 12:13 AM
Hi,

I'm using LibTiff to write TIFF files (from multiple threads) in C++, and getting
the following error message:

"Probable I/O race condition detected while copying memory. The I/O
package is not thread safe by default. In multithreaded applications,
a stream must be accessed in a thread-safe way, such as a thread-safe
wrapper returned by TeaxtReader´s or TextWriters´s Synchronized methods.
This also applies to classes like StreamWriter and StreamReader"

(The C++ is being called from a C# GUI, which accounts for the .NET class names.)

The weird thing is that the LibTiff call is in a critical section, which
(theoretically) should avoid race conditions:

DLLEXPORT int writeVariableImage (HBITMAP staticBlackLayer, HBITMAP varLayer,
VariableObject* varObj, int numVarObjects,
char* fileName)
{
if (!criticalSectionInitialized)
{
InitializeCriticalSection (&cs);
criticalSectionInitialized = true;
}

EnterCriticalSection(&cs);

try { // Make copy of black
if (!copyBitmap (varLayer, staticBlackLayer)) // layer to mark up:
throw 666; // Problem found.
HDC memDc = CreateCompatibleDC (NULL); // Prepare to draw:
HGDIOBJ Obmp = SelectObject(memDc, varLayer);

if (!memDc || !Obmp)
throw 666; // Problem found.
// Go through objects:
for (int i = 0; i < numVarObjects; ++i) // Draw markup:
drawVariableObject (memDc, &varObj [i]);// Draw next var object.
invertLayer (memDc, varLayer);
writeToTiffFile (varLayer, fileName); // Write to file.
if (!SelectObject (memDc, Obmp) || // Clean up:
!DeleteDC (memDc))
throw 666; // Problem found.
}
catch (...)
{
DWORD err = GetLastError ();

if (err)
{
LPTSTR s; // Show error message:
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL, err, 0, (LPTSTR)&s, 0, NULL);
MessageBox (NULL, s, L"Error", 0);
LocalFree (s);
}

LeaveCriticalSection(&cs);
return 0; // Failed.
}

LeaveCriticalSection(&cs);
return 1; // Success.
}
The other oddity is that the error only shows up on 1 out of the 4 computers it's
been tried on. (The computer that shows the error has 4 cores, the others, 2.)

Any ideas what could be causing this, or how to avoid it? Thanks!