From 72f758c433d9a8ece82404a711b474aae8c7cf7e Mon Sep 17 00:00:00 2001 From: daan Date: Fri, 6 Mar 2020 16:43:39 -0800 Subject: [PATCH] fix issue #210 where multiple static instances of mimalloc in DLL's compete for the same virtual memory area --- src/os.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/os.c b/src/os.c index 0aa85bd6..89fd349b 100644 --- a/src/os.c +++ b/src/os.c @@ -209,7 +209,12 @@ static void* mi_win_virtual_allocx(void* addr, size_t size, size_t try_alignment // on 64-bit systems, try to use the virtual address area after 4TiB for 4MiB aligned allocations void* hint; if (addr == NULL && (hint = mi_os_get_aligned_hint(try_alignment,size)) != NULL) { - return VirtualAlloc(hint, size, flags, PAGE_READWRITE); + void* p = VirtualAlloc(hint, size, flags, PAGE_READWRITE); + if (p != NULL) return p; + DWORD err = GetLastError(); + if (err != ERROR_INVALID_ADDRESS) { // if linked with multiple instances, we may have tried to allocate at an already allocated area + return NULL; + } } #endif #if defined(MEM_EXTENDED_PARAMETER_TYPE_BITS)