freopen and fopen not thread safe?
I'm getting a crash in printf saying the stdout pointer is invalid. I think
that freopen and fopen are not thread safe when used with one of the three
pre-defined streams (stdin, stdout, stderr).
I posted a feedback to Microsoft at
https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=449513
I'm confused with their answer of "This issue is by design. It's not that
the functions are not thread safe. The problem is that you're bypassing the
CRT initialization using WinMain. "
I was hoping some of you could might be able to elaborate better for me or
validate the bug in the connect website.
The code that reproduces this issue for me is:
#include <stdio.h>
#include <tchar.h>
#include <windows.h>
#include <crtdbg.h>
DWORD WINAPI freopenThread( LPVOID lpParam )
{
while (true)
{
freopen("CONOUT$", "w", stdout);
}
}
DWORD WINAPI fopenThread( LPVOID lpParam )
{
while (true)
{
FILE * f = fopen("c:\\valid.txt", "w");
if (f == stdout) // fopen should never return stdout
{
__asm { int 3 };
}
fclose(f);
}
}
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow
)
{
CreateThread( NULL, 0, fopenThread, NULL, 0, NULL);
CreateThread( NULL, 0, freopenThread, NULL, 0, NULL);
while(true) { }
return 0;
}
Thanks
-Patrick