From 81131bd844e16847e3e8b41f33b9627e8d32d419 Mon Sep 17 00:00:00 2001 From: Bastiaan Olij Date: Tue, 6 Apr 2021 11:55:15 +1000 Subject: Only cleanup meta data if GDNative library is reloadable and we're about to unload it --- modules/gdnative/nativescript/nativescript.cpp | 60 ++++++++++++++------------ 1 file changed, 32 insertions(+), 28 deletions(-) (limited to 'modules/gdnative') diff --git a/modules/gdnative/nativescript/nativescript.cpp b/modules/gdnative/nativescript/nativescript.cpp index 2a3202c0b8..f795bef59f 100644 --- a/modules/gdnative/nativescript/nativescript.cpp +++ b/modules/gdnative/nativescript/nativescript.cpp @@ -1727,44 +1727,48 @@ void NativeScriptLanguage::unregister_script(NativeScript *script) { if (S->get().size() == 0) { library_script_users.erase(S); - Map>::Element *L = library_classes.find(script->lib_path); - if (L) { - Map classes = L->get(); - - for (Map::Element *C = classes.front(); C; C = C->next()) { - // free property stuff first - for (OrderedHashMap::Element P = C->get().properties.front(); P; P = P.next()) { - if (P.get().getter.free_func) { - P.get().getter.free_func(P.get().getter.method_data); + Map>::Element *G = library_gdnatives.find(script->lib_path); + if (G && G->get()->get_library()->is_reloadable()) { + // ONLY if the library is marked as reloadable, and no more instances of its scripts exist do we unload the library + + // First remove meta data related to the library + Map>::Element *L = library_classes.find(script->lib_path); + if (L) { + Map classes = L->get(); + + for (Map::Element *C = classes.front(); C; C = C->next()) { + // free property stuff first + for (OrderedHashMap::Element P = C->get().properties.front(); P; P = P.next()) { + if (P.get().getter.free_func) { + P.get().getter.free_func(P.get().getter.method_data); + } + + if (P.get().setter.free_func) { + P.get().setter.free_func(P.get().setter.method_data); + } } - if (P.get().setter.free_func) { - P.get().setter.free_func(P.get().setter.method_data); + // free method stuff + for (Map::Element *M = C->get().methods.front(); M; M = M->next()) { + if (M->get().method.free_func) { + M->get().method.free_func(M->get().method.method_data); + } } - } - // free method stuff - for (Map::Element *M = C->get().methods.front(); M; M = M->next()) { - if (M->get().method.free_func) { - M->get().method.free_func(M->get().method.method_data); + // free constructor/destructor + if (C->get().create_func.free_func) { + C->get().create_func.free_func(C->get().create_func.method_data); } - } - // free constructor/destructor - if (C->get().create_func.free_func) { - C->get().create_func.free_func(C->get().create_func.method_data); + if (C->get().destroy_func.free_func) { + C->get().destroy_func.free_func(C->get().destroy_func.method_data); + } } - if (C->get().destroy_func.free_func) { - C->get().destroy_func.free_func(C->get().destroy_func.method_data); - } + library_classes.erase(script->lib_path); } - library_classes.erase(script->lib_path); - } - - Map>::Element *G = library_gdnatives.find(script->lib_path); - if (G && G->get()->get_library()->is_reloadable()) { + // now unload the library G->get()->terminate(); library_gdnatives.erase(G); } -- cgit v1.2.3