Called when the process is done (through `at_exit`)
| 622 | |
| 623 | // Called when the process is done (through `at_exit`) |
| 624 | static void mi_cdecl mi_process_done(void) { |
| 625 | // only shutdown if we were initialized |
| 626 | if (!_mi_process_is_initialized) return; |
| 627 | // ensure we are called once |
| 628 | static bool process_done = false; |
| 629 | if (process_done) return; |
| 630 | process_done = true; |
| 631 | |
| 632 | #if defined(_WIN32) && !defined(MI_SHARED_LIB) |
| 633 | FlsFree(mi_fls_key); // call thread-done on all threads (except the main thread) to prevent dangling callback pointer if statically linked with a DLL; Issue #208 |
| 634 | #endif |
| 635 | |
| 636 | #ifndef MI_SKIP_COLLECT_ON_EXIT |
| 637 | #if (MI_DEBUG != 0) || !defined(MI_SHARED_LIB) |
| 638 | // free all memory if possible on process exit. This is not needed for a stand-alone process |
| 639 | // but should be done if mimalloc is statically linked into another shared library which |
| 640 | // is repeatedly loaded/unloaded, see issue #281. |
| 641 | mi_collect(true /* force */ ); |
| 642 | #endif |
| 643 | #endif |
| 644 | |
| 645 | // Forcefully release all retained memory; this can be dangerous in general if overriding regular malloc/free |
| 646 | // since after process_done there might still be other code running that calls `free` (like at_exit routines, |
| 647 | // or C-runtime termination code. |
| 648 | if (mi_option_is_enabled(mi_option_destroy_on_exit)) { |
| 649 | _mi_heap_destroy_all(); // forcefully release all memory held by all heaps (of this thread only!) |
| 650 | _mi_segment_cache_free_all(&_mi_heap_main_get()->tld->os); // release all cached segments |
| 651 | } |
| 652 | |
| 653 | if (mi_option_is_enabled(mi_option_show_stats) || mi_option_is_enabled(mi_option_verbose)) { |
| 654 | mi_stats_print(NULL); |
| 655 | } |
| 656 | mi_allocator_done(); |
| 657 | _mi_verbose_message("process done: 0x%zx\n", _mi_heap_main.thread_id); |
| 658 | os_preloading = true; // don't call the C runtime anymore |
| 659 | } |
| 660 | |
| 661 | |
| 662 |
no test coverage detected