Merge pull request #79 from PartialVolume/deallocate_memory_used_by_logging.c

De-allocate memory used by the log functions
This commit is contained in:
Martijn van Brummelen
2018-11-23 10:38:38 +01:00
committed by GitHub
3 changed files with 25 additions and 3 deletions

View File

@@ -188,7 +188,7 @@ void nwipe_log( nwipe_log_t level, const char* format, ... )
}
}
/* Increase the current log element pointer - we will write here */
/* Increase the current log element pointer - we will write here, deallocation is done in cleanup() in nwipe.c */
if (log_current_element == log_elements_allocated) {
log_elements_allocated++;
result = realloc (log_lines, (log_elements_allocated) * sizeof(char *));
@@ -200,6 +200,7 @@ void nwipe_log( nwipe_log_t level, const char* format, ... )
}
log_lines = result;
/* Allocate memory for storing a single log message, deallocation is done in cleanup() in nwipe.c */
message_buffer_length = strlen( message_buffer ) * sizeof(char);
malloc_result = malloc((message_buffer_length + 1) * sizeof(char));
if (malloc_result == NULL)

View File

@@ -102,6 +102,7 @@ int main( int argc, char** argv )
nwipe_enumerated = nwipe_device_get( &c1, argv, argc );
if ( nwipe_enumerated == 0 )
{
cleanup();
exit(1);
}
@@ -210,6 +211,7 @@ int main( int argc, char** argv )
if( nwipe_options.nogui )
{
printf("--nogui option must be used with autonuke option\n");
cleanup();
exit(1);
}
else
@@ -481,6 +483,8 @@ int main( int argc, char** argv )
printf("%s\n", log_lines[i]);
}
cleanup();
/* Success. */
return 0;
@@ -604,8 +608,7 @@ void *signal_hand(void *ptr)
printf("Program interrupted (caught signal %d)\n", sig);
// Cleanup
// TODO: All other cleanup required
cleanup();
exit(0);
@@ -619,5 +622,20 @@ void *signal_hand(void *ptr)
} /* end of signal_hand */
int cleanup()
{
/* Deallocate memory used by logging */
int idx;
for ( idx=0; idx < log_elements_allocated; idx++ )
{
free ( log_lines[idx] );
}
log_elements_allocated=0; /* zeroed just in case cleanup is called twice */
free ( log_lines );
/* TODO: All other cleanup required */
return 0;
}
/* eof */

View File

@@ -20,6 +20,9 @@
*
*/
/* Function prototypes */
int cleanup();
#ifndef NWIPE_H_
#define NWIPE_H_