Skip to content

Commit

Permalink
Free up allocated objects upon early exit
Browse files Browse the repository at this point in the history
  • Loading branch information
nasajoey committed Jan 2, 2024
1 parent 5e61922 commit 134688e
Showing 1 changed file with 57 additions and 7 deletions.
64 changes: 57 additions & 7 deletions src/dw_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 134688e

Please sign in to comment.