summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorDaan Leijen <daan@microsoft.com>2022-04-07 12:33:25 -0700
committerDaan Leijen <daan@microsoft.com>2022-04-07 12:33:25 -0700
commita799b214a2429908cbfcb6e083b1606869c96696 (patch)
tree7c42226570dd2575e77f73e043054487c9df0e2f /src
parent58af58d084d57e43690ae78d0b8dad4303090096 (diff)
fix issue with log messages sometimes failing on Windows if the console cannot be locked; use direct console output now
Diffstat (limited to 'src')
-rw-r--r--src/options.c14
-rw-r--r--src/os.c9
2 files changed, 14 insertions, 9 deletions
diff --git a/src/options.c b/src/options.c
index 2022d96..e1944a1 100644
--- a/src/options.c
+++ b/src/options.c
@@ -163,10 +163,22 @@ void mi_option_disable(mi_option_t option) {
static void mi_out_stderr(const char* msg, void* arg) {
MI_UNUSED(arg);
+ if (msg == NULL) return;
#ifdef _WIN32
// on windows with redirection, the C runtime cannot handle locale dependent output
// after the main thread closes so we use direct console output.
- if (!_mi_preloading()) { _cputs(msg); }
+ if (!_mi_preloading()) {
+ // _cputs(msg); // _cputs cannot be used at is aborts if it fails to lock the console
+ static HANDLE hcon = INVALID_HANDLE_VALUE;
+ if (hcon == INVALID_HANDLE_VALUE) {
+ hcon = GetStdHandle(STD_ERROR_HANDLE);
+ }
+ const size_t len = strlen(msg);
+ if (hcon != INVALID_HANDLE_VALUE && len > 0 && len < UINT32_MAX) {
+ DWORD written = 0;
+ WriteConsoleA(hcon, msg, (DWORD)len, &written, NULL);
+ }
+ }
#else
fputs(msg, stderr);
#endif
diff --git a/src/os.c b/src/os.c
index 62c98da..52939fa 100644
--- a/src/os.c
+++ b/src/os.c
@@ -343,15 +343,8 @@ static void* mi_win_virtual_allocx(void* addr, size_t size, size_t try_alignment
if (hint != NULL) {
void* p = VirtualAlloc(hint, size, flags, PAGE_READWRITE);
if (p != NULL) return p;
- // for robustness always fall through in case of an error
- /*
- DWORD err = GetLastError();
- if (err != ERROR_INVALID_ADDRESS && // If linked with multiple instances, we may have tried to allocate at an already allocated area (#210)
- err != ERROR_INVALID_PARAMETER) { // Windows7 instability (#230)
- return NULL;
- }
- */
_mi_warning_message("unable to allocate hinted aligned OS memory (%zu bytes, error code: 0x%x, address: %p, alignment: %zu, flags: 0x%x)\n", size, GetLastError(), hint, try_alignment, flags);
+ // fall through on error
}
}
#endif