diff --git a/python/tests/thread2.py b/python/tests/thread2.py index f7d2595b..9991b4ce 100755 --- a/python/tests/thread2.py +++ b/python/tests/thread2.py @@ -96,6 +96,9 @@ if failed: # Memory debug specific libxml2.cleanupParser() +# Note that this can leak memory on Windows if the global state +# destructors weren't run yet. They should be called eventually, +# so this leak should be harmless. if libxml2.debugMemory(1) == 0: print("OK") else: diff --git a/xmlmemory.c b/xmlmemory.c index 3b1ebaf0..1e999b11 100644 --- a/xmlmemory.c +++ b/xmlmemory.c @@ -878,7 +878,16 @@ xmlCleanupMemory(void) { */ void xmlCleanupMemoryInternal(void) { + /* + * Don't clean up mutex on Windows. Global state destructors can call + * malloc functions after xmlCleanupParser was called. If memory + * debugging is enabled, xmlMemMutex can be used after cleanup. + * + * See python/tests/thread2.py + */ +#if !defined(LIBXML_THREAD_ENABLED) || !defined(_WIN32) xmlCleanupMutex(&xmlMemMutex); +#endif } /**