From 0da98f95b876b22e6771de72591bcaabd2e7362e Mon Sep 17 00:00:00 2001 From: Jonathan Moore Liles Date: Tue, 30 Jul 2013 21:36:35 -0700 Subject: [PATCH] Fix some memory leaks and other valgrind warnings. --- FL/New_Project_Dialog.fl | 2 +- lib/ntk | 2 +- mixer/src/Chain.C | 3 +++ mixer/src/Mixer.C | 17 +++++++++------ nonlib/OSC/Endpoint.C | 21 ++++++++++++++---- nonlib/file.C | 16 ++++++++------ nonlib/file.h | 2 +- session-manager/src/session-manager.C | 31 ++++++++++++++------------- timeline/src/OSC_Thread.C | 5 ++++- timeline/src/Project.C | 5 ++++- timeline/src/TLE.fl | 18 ++++++++++------ timeline/src/Timeline.C | 8 ++++++- 12 files changed, 84 insertions(+), 46 deletions(-) diff --git a/FL/New_Project_Dialog.fl b/FL/New_Project_Dialog.fl index 906fcbe..14be033 100644 --- a/FL/New_Project_Dialog.fl +++ b/FL/New_Project_Dialog.fl @@ -126,7 +126,7 @@ nsd.default_path( *default_path ); nsd.run(); if ( nsd.default_path() ) - *default_path = strdup( nsd.default_path() ); + *default_path = nsd.default_path(); return nsd.path;} {} } diff --git a/lib/ntk b/lib/ntk index 415422b..d006352 160000 --- a/lib/ntk +++ b/lib/ntk @@ -1 +1 @@ -Subproject commit 415422bd1b35cdbac5c751dcf8f4fb9cbe696927 +Subproject commit d0063527aa360a3f1fd34e76fc0dd9125efb9202 diff --git a/mixer/src/Chain.C b/mixer/src/Chain.C index a9f7d94..3814983 100644 --- a/mixer/src/Chain.C +++ b/mixer/src/Chain.C @@ -176,6 +176,9 @@ Chain::~Chain ( ) engine()->lock(); + for ( unsigned int i = scratch_port.size(); i--; ) + delete[] (sample_t*)scratch_port[i].buffer(); + /* if we leave this up to FLTK, it will happen after we've already destroyed the engine */ modules_pack->clear(); diff --git a/mixer/src/Mixer.C b/mixer/src/Mixer.C index 221b223..b01824a 100644 --- a/mixer/src/Mixer.C +++ b/mixer/src/Mixer.C @@ -190,11 +190,11 @@ void Mixer::command_new ( void ) { DMESSAGE( "New project" ); - char *default_path; + char *default_path = read_line( user_config_dir, "default_path" ); - read_line( user_config_dir, "default_path", &default_path ); - - char *path = new_project_chooser( &default_path ); + char *result_path = default_path; + + char *path = new_project_chooser( &result_path ); if ( path ) { @@ -207,10 +207,13 @@ void Mixer::command_new ( void ) update_menu(); - if ( default_path ) + if ( result_path != default_path ) + free(default_path); + + if ( result_path ) { - write_line( user_config_dir, "default_path", default_path ); - free( default_path ); + write_line( user_config_dir, "default_path", result_path ); + free( result_path ); } } diff --git a/nonlib/OSC/Endpoint.C b/nonlib/OSC/Endpoint.C index 81fa418..ae4c25b 100644 --- a/nonlib/OSC/Endpoint.C +++ b/nonlib/OSC/Endpoint.C @@ -48,6 +48,8 @@ namespace OSC free( _path ); if ( _typespec ) free( _typespec ); + if ( _documentation ) + free( _documentation ); } /**********/ @@ -228,13 +230,25 @@ namespace OSC Endpoint::~Endpoint ( ) { // lo_server_thread_free( _st ); + + for ( std::list::iterator i = _methods.begin(); + i != _methods.end(); + i++ ) + delete(*i); + + _methods.clear(); + if ( _server ) { lo_server_free( _server ); _server = 0; } + + lo_address_free( _addr ); + _addr = 0; } + OSC::Signal * Endpoint::find_target_by_peer_address ( std::list *l, lo_address addr ) { @@ -947,14 +961,13 @@ namespace OSC Signal * Endpoint::add_signal ( const char *path, Signal::Direction dir, float min, float max, float default_value, signal_handler handler, void *user_data ) { - Signal *o = new Signal( path, dir ); - char *s; asprintf( &s, "%s%s", name(), path ); - if ( s ) - o->_path = s; + Signal *o = new Signal( s, dir ); + free(s); + o->_handler = handler; o->_user_data = user_data; o->_endpoint = this; diff --git a/nonlib/file.C b/nonlib/file.C index 42fb8ea..f861727 100644 --- a/nonlib/file.C +++ b/nonlib/file.C @@ -159,25 +159,27 @@ write_line ( const char *dir, const char *name, const char *value ) } /** write a single string to a file */ -void -read_line ( const char *dir, const char *name, char **value ) +char * +read_line ( const char *dir, const char *name ) { char path[512]; - *value = 0; - snprintf( path, sizeof( path ), "%s/%s", dir, name ); FILE *fp = fopen( path, "r" ); if ( ! fp ) - return; + return 0; - *value = (char*)malloc( 512 ); + char *value = (char*)malloc( 512 ); - fgets( *value, 512, fp ); + value[0] = 0; + + fgets( value, 512, fp ); fclose( fp ); + + return value; } #include diff --git a/nonlib/file.h b/nonlib/file.h index 3c95174..63aa4dd 100644 --- a/nonlib/file.h +++ b/nonlib/file.h @@ -30,7 +30,7 @@ int backwards_fgetc ( FILE *fp ); char * backwards_fgets ( char *s, int size, FILE *fp ); void touch ( int fd ); void write_line ( const char *dir, const char *name, const char *value ); -void read_line ( const char *dir, const char *name, char **value ); +char * read_line ( const char *dir, const char *name ); size_t free_space ( const char *file ); size_t total_space ( const char *file ); int percent_used ( const char *file ); diff --git a/session-manager/src/session-manager.C b/session-manager/src/session-manager.C index 37d02b0..185ce4b 100644 --- a/session-manager/src/session-manager.C +++ b/session-manager/src/session-manager.C @@ -144,7 +144,7 @@ class NSM_Client : public Fl_Group client_name->copy_label( l ); - // _client_label = l; + free(l); redraw(); } @@ -178,7 +178,7 @@ public: { if ( _client_id ) free( _client_id ); - + _client_id = strdup( v ); } @@ -240,11 +240,7 @@ public: void pending_command ( const char *command ) { - char *cmd = strdup( command ); - - free( (void*)_progress->label() ); - - _progress->label( cmd ); + _progress->copy_label( command ); stopped( 0 ); @@ -375,7 +371,7 @@ public: { Fl_Progress *o = _progress = new Fl_Progress( xx, Y + H * 0.25, 75, H * 0.50, NULL ); o->box( FL_FLAT_BOX ); o->color( FL_BLACK ); - o->label( strdup( "launch" ) ); + o->copy_label( "launch" ); o->labelsize( 12 ); o->minimum( 0.0f ); o->maximum( 1.0f ); @@ -462,6 +458,12 @@ public: ~NSM_Client ( ) { + if ( _client_id ) + { + free( _client_id ); + _client_id = NULL; + } + if ( _client_name ) { free( _client_name ); @@ -925,7 +927,7 @@ public: o->type( FL_VERTICAL ); o->spacing( 2 ); - { Fl_Box *o = new Fl_Box( 0,0,100, 24, "Sessions" ); + { new Fl_Box( 0,0,100, 24, "Sessions" ); } { @@ -1049,8 +1051,6 @@ public: osc->owner = this; - osc->url(); - osc->add_method( "/error", "sis", osc_handler, osc, "msg" ); osc->add_method( "/reply", "ss", osc_handler, osc, "msg" ); osc->add_method( "/reply", "s", osc_handler, osc, "" ); @@ -1398,14 +1398,13 @@ main (int argc, char **argv ) { /* pass non-option arguments on to daemon */ - char **args = (char **)malloc( 4 + argc - optind ); + char *args[4 + argc - optind]; int i = 0; - args[i++] = (char*)"nsmd"; - args[i++] = (char*)"--gui-url"; + args[i++] = strdup("nsmd"); + args[i++] = strdup("--gui-url"); args[i++] = url; - for ( ; optind < argc; i++, optind++ ) { DMESSAGE( "Passing argument: %s", argv[optind] ); @@ -1419,6 +1418,8 @@ main (int argc, char **argv ) FATAL( "Error starting process: %s", strerror( errno ) ); } } + + free(url); } Fl::add_timeout( 1.0, ping, NULL ); diff --git a/timeline/src/OSC_Thread.C b/timeline/src/OSC_Thread.C index 2f2acf0..70fe6fe 100644 --- a/timeline/src/OSC_Thread.C +++ b/timeline/src/OSC_Thread.C @@ -38,12 +38,15 @@ OSC_Thread::OSC_Thread ( ) OSC_Thread::~OSC_Thread ( ) { + lock(); if ( _shutdown == false ) { _shutdown = true; _thread.join(); } -} + unlock(); +} + void OSC_Thread::start ( ) diff --git a/timeline/src/Project.C b/timeline/src/Project.C index 1551693..299da5e 100644 --- a/timeline/src/Project.C +++ b/timeline/src/Project.C @@ -209,7 +209,7 @@ Project::close ( void ) *Project::_name = '\0'; *Project::_created_on = '\0'; - + release_lock( &_lockfd, ".lock" ); delete engine; @@ -326,6 +326,9 @@ Project::open ( const char *name ) else *_created_on = 0; + free( created_by ); + free( creation_date ); + set_name( name ); *_path = '\0'; diff --git a/timeline/src/TLE.fl b/timeline/src/TLE.fl index 13c8070..c3882a6 100644 --- a/timeline/src/TLE.fl +++ b/timeline/src/TLE.fl @@ -330,12 +330,13 @@ pi.run();} label {&New} callback {save_timeline_settings(); + + char *result_path; -char *default_path; + char *default_path = read_line( user_config_dir, "default_path" ); + result_path = default_path; - read_line( user_config_dir, "default_path", &default_path ); - - char *path = new_project_chooser( &default_path ); + char *path = new_project_chooser( &result_path ); if ( path ) { @@ -344,10 +345,13 @@ char *default_path; free( path ); } - if ( default_path ) + if ( reuslt_path != default_path ) + free(default_path); + + if ( result_path ) { - write_line( user_config_dir, "default_path", default_path ); - free( default_path ); + write_line( user_config_dir, "default_path", result_path ); + free( result_path ); } diff --git a/timeline/src/Timeline.C b/timeline/src/Timeline.C index f1e46de..657646b 100644 --- a/timeline/src/Timeline.C +++ b/timeline/src/Timeline.C @@ -395,6 +395,8 @@ Timeline::menu_cb ( Fl_Menu_ *m ) Track *t = new Track( name ); + free( name ); + Audio_Sequence *o = new Audio_Sequence( t ); add_track( t ); @@ -582,6 +584,8 @@ Timeline::~Timeline ( ) { delete osc_thread; osc_thread = 0; + delete osc; + osc = 0; } Timeline::Timeline ( int X, int Y, int W, int H, const char* L ) : BASE( X, Y, W, H, L ) @@ -2095,7 +2099,9 @@ Timeline::init_osc ( const char *osc_port ) osc->owner = this; - printf( "OSC=%s\n", osc->url() ); + char *url = osc->url(); + printf( "OSC=%s\n", url ); + free(url); osc->add_method( "/non/hello", "ssss", &Timeline::osc_non_hello, osc, "" );