@@ -446,6 +446,7 @@ void MenuSingleton::_bind_methods() {
446446
447447 /* BUDGET MENU */
448448 OV_BIND_METHOD (MenuSingleton::link_budget_menu_to_cpp, { " godot_budget_menu" });
449+ OV_BIND_METHOD (MenuSingleton::unlink_budget_menu_from_cpp);
449450
450451 /* Find/Search Panel */
451452 OV_BIND_METHOD (MenuSingleton::generate_search_cache);
@@ -1539,9 +1540,15 @@ String MenuSingleton::get_longform_date() const {
15391540void MenuSingleton::link_budget_menu_to_cpp (GUINode const * const godot_budget_menu) {
15401541 ERR_FAIL_NULL (godot_budget_menu);
15411542 GameSingleton& game_singleton = *GameSingleton::get_singleton ();
1542- BudgetMenu* old_instance = budget_menu.get ();
1543- if (old_instance != nullptr ) {
1544- game_singleton.gamestate_updated .disconnect (&BudgetMenu::update, old_instance);
1543+
1544+ if (budget_menu) {
1545+ UtilityFunctions::push_error (
1546+ " Trying to link new C++ and GDScript BudgetMenu instances without unlinking the old instances first! "
1547+ " The unlinking must happen just before the GDScript BudgetMenu is freed, "
1548+ " otherwise the C++ BudgetMenu will continue running despite all its UI node pointers now being invalid."
1549+ );
1550+
1551+ unlink_budget_menu_from_cpp ();
15451552 }
15461553
15471554 auto const & strata_keys = game_singleton.get_definition_manager ().get_pop_manager ().get_stratas ();
@@ -1556,6 +1563,15 @@ void MenuSingleton::link_budget_menu_to_cpp(GUINode const* const godot_budget_me
15561563 game_singleton.gamestate_updated .connect (&BudgetMenu::update, budget_menu.get ());
15571564}
15581565
1566+ void MenuSingleton::unlink_budget_menu_from_cpp () {
1567+ if (budget_menu) {
1568+ GameSingleton::get_singleton ()->gamestate_updated .disconnect (&BudgetMenu::update, budget_menu.get ());
1569+ budget_menu.reset ();
1570+ } else {
1571+ UtilityFunctions::push_warning (" unlink_budget_menu_from_cpp called but no C++ BudgetMenu instance was linked!" );
1572+ }
1573+ }
1574+
15591575/* Find/Search Panel */
15601576
15611577Error MenuSingleton::generate_search_cache () {
0 commit comments