The call to fclose
is failing because you are passing a null FILE*
. The FILE*
is null because the call to freopen_s
fails. That call fails because the process has no console to be opened.
Your program works in debug builds because you call AllocConsole
to allocate a console for your process. Your program does not work in release builds because you do not call AllocConsole
; you call AttachConsole
, which will fail (and have no effect) if the parent process has no console.