From 6bf993decaa5c52cf75ac9d187cdaaed4ec95614 Mon Sep 17 00:00:00 2001 From: Arnaud Giersch Date: Tue, 2 Apr 2019 15:37:55 +0200 Subject: [PATCH] Fix memleaks when exiting early (e.g. with --help or --version). --- src/include/simgrid/sg_config.hpp | 1 - src/simdag/sd_global.cpp | 7 ++----- src/simgrid/sg_config.cpp | 26 +++++++------------------- src/simix/smx_global.cpp | 7 ++----- src/surf/surf_interface.cpp | 7 +++++-- src/surf/surf_interface.hpp | 8 ++++---- 6 files changed, 20 insertions(+), 36 deletions(-) diff --git a/src/include/simgrid/sg_config.hpp b/src/include/simgrid/sg_config.hpp index 5e602a5128..a9c8c10086 100644 --- a/src/include/simgrid/sg_config.hpp +++ b/src/include/simgrid/sg_config.hpp @@ -11,7 +11,6 @@ /** Config Globals */ XBT_PUBLIC_DATA int _sg_cfg_init_status; -XBT_PUBLIC_DATA bool _sg_cfg_exit_asap; XBT_PUBLIC void sg_config_init(int* argc, char** argv); XBT_PUBLIC void sg_config_finalize(); diff --git a/src/simdag/sd_global.cpp b/src/simdag/sd_global.cpp index bc5f199549..f6974f9eda 100644 --- a/src/simdag/sd_global.cpp +++ b/src/simdag/sd_global.cpp @@ -157,16 +157,13 @@ void SD_init_nocheck(int *argc, char **argv) { xbt_assert(sd_global == nullptr, "SD_init() already called"); - sd_global = new simgrid::sd::Global(); - surf_init(argc, argv); + sd_global = new simgrid::sd::Global(); + simgrid::config::set_default("host/model", "ptask_L07"); if (simgrid::config::get_value("clean-atexit")) atexit(SD_exit); - if (_sg_cfg_exit_asap) { - exit(0); - } } /** @brief set a configuration variable diff --git a/src/simgrid/sg_config.cpp b/src/simgrid/sg_config.cpp index 6358b596a1..d479532be7 100644 --- a/src/simgrid/sg_config.cpp +++ b/src/simgrid/sg_config.cpp @@ -41,15 +41,6 @@ void sg_config_continue_after_help() */ int _sg_cfg_init_status = 0; -/* instruct the upper layer (simix or simdag) to exit as soon as possible */ -bool _sg_cfg_exit_asap = false; - -#define sg_cfg_exit_early() \ - do { \ - _sg_cfg_exit_asap = true; \ - return; \ - } while (0) - /* Parse the command line, looking for options */ static void sg_config_cmd_line(int *argc, char **argv) { @@ -120,7 +111,7 @@ static void sg_config_cmd_line(int *argc, char **argv) *argc = j; } if (shall_exit) - sg_cfg_exit_early(); + exit(0); } /* callback of the plugin variable */ @@ -133,7 +124,7 @@ static void _sg_cfg_cb__plugin(const std::string& value) if (value == "help") { model_help("plugin", surf_plugin_description); - sg_cfg_exit_early(); + exit(0); } int plugin_id = find_model_description(surf_plugin_description, value); @@ -147,7 +138,7 @@ static void _sg_cfg_cb__host_model(const std::string& value) if (value == "help") { model_help("host", surf_host_model_description); - sg_cfg_exit_early(); + exit(0); } /* Make sure that the model exists */ @@ -161,7 +152,7 @@ static void _sg_cfg_cb__cpu_model(const std::string& value) if (value == "help") { model_help("CPU", surf_cpu_model_description); - sg_cfg_exit_early(); + exit(0); } /* New Module missing */ @@ -175,7 +166,7 @@ static void _sg_cfg_cb__optimization_mode(const std::string& value) if (value == "help") { model_help("optimization", surf_optimization_mode_description); - sg_cfg_exit_early(); + exit(0); } /* New Module missing */ @@ -189,7 +180,7 @@ static void _sg_cfg_cb__storage_mode(const std::string& value) if (value == "help") { model_help("storage", surf_storage_model_description); - sg_cfg_exit_early(); + exit(0); } find_model_description(surf_storage_model_description, value); @@ -202,7 +193,7 @@ static void _sg_cfg_cb__network_model(const std::string& value) if (value == "help") { model_help("network", surf_network_model_description); - sg_cfg_exit_early(); + exit(0); } /* New Module missing */ @@ -489,9 +480,6 @@ void sg_config_init(int *argc, char **argv) void sg_config_finalize() { - if (not _sg_cfg_init_status) - return; /* Not initialized yet. Nothing to do */ - simgrid::config::finalize(); _sg_cfg_init_status = 0; } diff --git a/src/simix/smx_global.cpp b/src/simix/smx_global.cpp index 4a300d811c..b463c5a153 100644 --- a/src/simix/smx_global.cpp +++ b/src/simix/smx_global.cpp @@ -192,10 +192,10 @@ void SIMIX_global_init(int *argc, char **argv) #endif if (simix_global == nullptr) { + surf_init(argc, argv); /* Initialize SURF structures */ + simix_global.reset(new simgrid::simix::Global()); simix_global->maestro_process = nullptr; - - surf_init(argc, argv); /* Initialize SURF structures */ SIMIX_context_mod_init(); // Either create a new context with maestro or create @@ -220,9 +220,6 @@ void SIMIX_global_init(int *argc, char **argv) if (simgrid::config::get_value("clean-atexit")) atexit(SIMIX_clean); - - if (_sg_cfg_exit_asap) - exit(0); } int smx_cleaned = 0; diff --git a/src/surf/surf_interface.cpp b/src/surf/surf_interface.cpp index 8beb759567..d2f6966f03 100644 --- a/src/surf/surf_interface.cpp +++ b/src/surf/surf_interface.cpp @@ -37,6 +37,11 @@ std::set watched_hosts; extern std::map storage_types; s_surf_model_description_t* surf_plugin_description = nullptr; +static void XBT_ATTRIB_DESTRUCTOR(800) simgrid_free_plugin_description() +{ + xbt_free(surf_plugin_description); +} + XBT_PUBLIC void simgrid_add_plugin_description(const char* name, const char* description, void_f_void_t init_fun) { static int plugin_amount = 0; @@ -314,8 +319,6 @@ void surf_exit() for (auto const& model : all_existing_models) delete model; - xbt_free(surf_plugin_description); - tmgr_finalize(); sg_platf_exit(); diff --git a/src/surf/surf_interface.hpp b/src/surf/surf_interface.hpp index 6a602ccbf8..38fad6258e 100644 --- a/src/surf/surf_interface.hpp +++ b/src/surf/surf_interface.hpp @@ -216,10 +216,10 @@ typedef struct surf_model_description s_surf_model_description_t; XBT_PUBLIC int find_model_description(s_surf_model_description_t* table, const std::string& name); XBT_PUBLIC void model_help(const char* category, s_surf_model_description_t* table); -#define SIMGRID_REGISTER_PLUGIN(id, desc, init) \ - void simgrid_##id##_plugin_register(); \ - void XBT_ATTRIB_CONSTRUCTOR(800) simgrid_##id##_plugin_register() { \ - simgrid_add_plugin_description(#id, desc, init); \ +#define SIMGRID_REGISTER_PLUGIN(id, desc, init) \ + static void XBT_ATTRIB_CONSTRUCTOR(800) simgrid_##id##_plugin_register() \ + { \ + simgrid_add_plugin_description(#id, desc, init); \ } XBT_PUBLIC void simgrid_add_plugin_description(const char* name, const char* description, void_f_void_t init_fun); -- 2.20.1