I am having troubles with FreeConsole
/AllocConsole
and their connection with stdio (stdin
/stdout
/stderr
standard file pointers). I have successfully used this sequence for years:
int main(int argc, char **argv)
{
FreeConsole();
AllocConsole();
freopen("CONIN$", "rb", stdin);
freopen("CONOUT$", "wb", stdout);
freopen("CONOUT$", "wb", stderr);
(see this question for example). However, on Windows10 with latest updates I am getting exception 0xC0000008 in freopen
. Note that
- I can see it only during a debugging sessions (VS2017, Debug configuration). Normal application launch, and Release configuration too, seem to work (or maybe the exception is silently ignored?).
- It happens very often, but not in a repeteable way. Sometimes the first freopen fails, sometimes the second one, sometimes the third one, sometimes none o them.
- The debugger intercepts the exception (I know, I could turn it off, but I would prefer not to since it is an interesting event in general). When I force program continuation, the program seems to work correctly. However, I am not at all sure that the CRT state is still safe.
- If I add
fclose(stdin); fclose(stdout); fclose(stderr)
beforeFreeConsole
, the exception happens inFreeConsole
.
Questions:
- Is there something wrong in the sequence above?
- What is the correct way to connect standard file pointers to a new console?
- Is there a way to avoid this exception (i.e. make sure that it doesn't happen, not just silently ignore)? It is quite annoying in my debugging sessions.
More information:
- I am using VS2017, but the program uses VS2013 toolset.
- The program is compiled for x86 (32 bits).
- In both Debug and Release configuration DLL-Multithread (/MD) library and Multibyte character set are used.
- OS is Windows10-64 Professional 1903 - 18362.720
Thak you in advance