fix(AGraph): unref transient GtkBuilder on all menu-build paths
The GtkBuilder created for menu construction was never unref'd on any path (success or throw), leaking one builder per graph module with a menu. Unref on each throw and at the end, and take an explicit ref on menu_ so it survives dropping the builder (mirrors ALabel).
This commit is contained in:
@@ -79,13 +79,17 @@ AGraph::AGraph(const Json::Value& config, const std::string& name, const std::st
|
|||||||
|
|
||||||
// Make the GtkBuilder and check for errors in his parsing
|
// Make the GtkBuilder and check for errors in his parsing
|
||||||
if (gtk_builder_add_from_string(builder, fileContent.str().c_str(), -1, nullptr) == 0U) {
|
if (gtk_builder_add_from_string(builder, fileContent.str().c_str(), -1, nullptr) == 0U) {
|
||||||
|
g_object_unref(builder);
|
||||||
throw std::runtime_error("Error found in the file " + menuFile);
|
throw std::runtime_error("Error found in the file " + menuFile);
|
||||||
}
|
}
|
||||||
|
|
||||||
menu_ = gtk_builder_get_object(builder, "menu");
|
menu_ = gtk_builder_get_object(builder, "menu");
|
||||||
if (menu_ == nullptr) {
|
if (menu_ == nullptr) {
|
||||||
|
g_object_unref(builder);
|
||||||
throw std::runtime_error("Failed to get 'menu' object from GtkBuilder");
|
throw std::runtime_error("Failed to get 'menu' object from GtkBuilder");
|
||||||
}
|
}
|
||||||
|
// Keep the menu alive after dropping the transient GtkBuilder.
|
||||||
|
g_object_ref(menu_);
|
||||||
submenus_ = std::map<std::string, GtkMenuItem*>();
|
submenus_ = std::map<std::string, GtkMenuItem*>();
|
||||||
menuActionsMap_ = std::map<std::string, std::string>();
|
menuActionsMap_ = std::map<std::string, std::string>();
|
||||||
|
|
||||||
@@ -98,6 +102,7 @@ AGraph::AGraph(const Json::Value& config, const std::string& name, const std::st
|
|||||||
g_signal_connect(submenus_[key], "activate", G_CALLBACK(handleGtkMenuEvent),
|
g_signal_connect(submenus_[key], "activate", G_CALLBACK(handleGtkMenuEvent),
|
||||||
(gpointer)menuActionsMap_[key].c_str());
|
(gpointer)menuActionsMap_[key].c_str());
|
||||||
}
|
}
|
||||||
|
g_object_unref(builder);
|
||||||
} catch (std::runtime_error& e) {
|
} catch (std::runtime_error& e) {
|
||||||
spdlog::warn("Error while creating the menu : {}. Menu popup not activated.", e.what());
|
spdlog::warn("Error while creating the menu : {}. Menu popup not activated.", e.what());
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user