From 134688e5a4c47d91d40067e975ff5919f632e0e6 Mon Sep 17 00:00:00 2001 From: Joseph Rios Date: Tue, 2 Jan 2024 14:33:10 -0800 Subject: [PATCH] Free up allocated objects upon early exit --- src/dw_main.c | 64 +++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 57 insertions(+), 7 deletions(-) diff --git a/src/dw_main.c b/src/dw_main.c index ea38c52..ad7ed02 100644 --- a/src/dw_main.c +++ b/src/dw_main.c @@ -91,6 +91,14 @@ #include "dw_phases.h" static int hook(void* info, const char* s); +void free_objects( subprob_struct* sub_data, + faux_globals* globals, + master_data* md, + int* ind, + double* val, + pthread_t* threads, + glp_smcp* simplex_control_params, + char* local_buffer); /* See process_cmdline() in support_functions.c for parameters passed to main. */ @@ -127,16 +135,35 @@ int main(int argc, char* argv[]) { /* Process the command line. */ rc = process_cmdline(argc, argv, globals); - if( rc == 1 ) { - dw_printf(IMPORTANCE_VITAL, - "Something wrong with command line, exiting.\n"); - return 1; + + /* Non-zero indicates we shouldn't go on. If it was 2, we printed 'help' message. + If some other non-zero, then there was a problem. In either case, free up + allocated memory. */ + if( rc != 0 ) { + free_objects( + sub_data, + globals, + md, + ind, + val, + threads, + simplex_control_params, + local_buffer + ); + if( rc != 2 ) { + dw_printf(IMPORTANCE_VITAL, "Something wrong with command line, exiting.\n"); + return 1; + } + else return 0; } - else if( rc == 2 ) { + + if( rc == 2 ) { /* Help flag was sent in, printed help, now exit. */ + return 0; } - else if( rc != 0 ) { - return rc; + else if( rc != 0 ) { /* Something actually wrong, exit. */ + dw_printf(IMPORTANCE_VITAL, "Something wrong with command line, exiting.\n"); + return 1; } dw_printf(IMPORTANCE_AVG,"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n"); @@ -815,6 +842,29 @@ int main(int argc, char* argv[]) { return 0; } +void free_objects( + subprob_struct* sub_data, + faux_globals* globals, + master_data* md, + int* ind, + double* val, + pthread_t* threads, + glp_smcp* simplex_control_params, + char* local_buffer +) { + /* Clean up after ourselves. */ + free_sub_data(sub_data, globals); + free_globals(globals, md); + free(globals); + free(ind); + free(val); + free(threads); + free(simplex_control_params); + free(local_buffer); + return; +} + + /* The function that glp_term_hook uses to redirect terminal output */ static int hook(void* info, const char* s) { FILE* outfile = info;