Merge pull request #575 from manupap1/memory_fix

Close logger and database on exit
pull/576/merge
Isaac Connor 2014-11-26 15:08:21 -05:00
commit 51703f5c37
10 changed files with 54 additions and 1 deletions

View File

@ -64,3 +64,14 @@ void zmDbConnect()
zmDbConnected = true; zmDbConnected = true;
} }
void zmDbClose()
{
if ( zmDbConnected )
{
mysql_close( &dbconn );
// mysql_init() call implicitly mysql_library_init() but
// mysql_close() does not call mysql_library_end()
mysql_library_end();
zmDbConnected = false;
}
}

View File

@ -30,6 +30,8 @@ extern MYSQL dbconn;
extern int zmDbConnected; extern int zmDbConnected;
void zmDbConnect(); void zmDbConnect();
void zmDbClose();
#ifdef __cplusplus #ifdef __cplusplus
} /* extern "C" */ } /* extern "C" */
#endif #endif

View File

@ -239,6 +239,9 @@ void Logger::terminate()
if ( mSyslogLevel > NOLOG ) if ( mSyslogLevel > NOLOG )
closeSyslog(); closeSyslog();
if ( mDatabaseLevel > NOLOG )
closeDatabase();
} }
bool Logger::boolEnv( const std::string &name, bool defaultValue ) bool Logger::boolEnv( const std::string &name, bool defaultValue )
@ -468,6 +471,15 @@ void Logger::closeFile()
} }
} }
void Logger::closeDatabase()
{
if ( mDbConnected )
{
mysql_close( &mDbConnection );
mDbConnected = false;
}
}
void Logger::openSyslog() void Logger::openSyslog()
{ {
(void) openlog( mId.c_str(), LOG_PID|LOG_NDELAY, LOG_LOCAL1 ); (void) openlog( mId.c_str(), LOG_PID|LOG_NDELAY, LOG_LOCAL1 );
@ -604,5 +616,6 @@ void logInit( const char *name, const Logger::Options &options )
void logTerm() void logTerm()
{ {
Logger::fetch()->terminate(); if ( Logger::smInstance )
delete Logger::smInstance;
} }

View File

@ -115,6 +115,7 @@ private:
public: public:
friend void logInit( const char *name, const Options &options ); friend void logInit( const char *name, const Options &options );
friend void logTerm();
static Logger *fetch() static Logger *fetch()
{ {
@ -182,6 +183,7 @@ private:
void closeFile(); void closeFile();
void openSyslog(); void openSyslog();
void closeSyslog(); void closeSyslog();
void closeDatabase();
public: public:
void logPrint( bool hex, const char * const file, const int line, const int level, const char *fstring, ... ); void logPrint( bool hex, const char * const file, const int line, const int level, const char *fstring, ... );

View File

@ -137,5 +137,7 @@ int main( int argc, char *argv[] )
{ {
fprintf( stderr, "Can't find monitor with id of %d\n", id ); fprintf( stderr, "Can't find monitor with id of %d\n", id );
} }
logTerm();
zmDbClose();
return( 0 ); return( 0 );
} }

View File

@ -302,5 +302,8 @@ int main( int argc, char *argv[] )
delete [] next_delays; delete [] next_delays;
delete [] last_capture_times; delete [] last_capture_times;
logTerm();
zmDbClose();
return( result ); return( result );
} }

View File

@ -307,4 +307,6 @@ int main( int argc, char *argv[] )
sigprocmask( SIG_UNBLOCK, &block_set, 0 ); sigprocmask( SIG_UNBLOCK, &block_set, 0 );
} }
logTerm();
zmDbClose();
} }

View File

@ -223,6 +223,8 @@ int main( int argc, const char *argv[] )
if ( !user ) if ( !user )
{ {
Error( "Unable to authenticate user" ); Error( "Unable to authenticate user" );
logTerm();
zmDbClose();
return( -1 ); return( -1 );
} }
ValidateAccess( user, monitor_id ); ValidateAccess( user, monitor_id );
@ -286,6 +288,8 @@ int main( int argc, const char *argv[] )
#else // HAVE_LIBAVCODEC #else // HAVE_LIBAVCODEC
Error( "MPEG streaming of '%s' attempted while disabled", query ); Error( "MPEG streaming of '%s' attempted while disabled", query );
fprintf( stderr, "MPEG streaming is disabled.\nYou should configure with the --with-ffmpeg option and rebuild to use this functionality.\n" ); fprintf( stderr, "MPEG streaming is disabled.\nYou should configure with the --with-ffmpeg option and rebuild to use this functionality.\n" );
logTerm();
zmDbClose();
return( -1 ); return( -1 );
#endif // HAVE_LIBAVCODEC #endif // HAVE_LIBAVCODEC
} }
@ -320,10 +324,16 @@ int main( int argc, const char *argv[] )
#else // HAVE_LIBAVCODEC #else // HAVE_LIBAVCODEC
Error( "MPEG streaming of '%s' attempted while disabled", query ); Error( "MPEG streaming of '%s' attempted while disabled", query );
fprintf( stderr, "MPEG streaming is disabled.\nYou should ensure the ffmpeg libraries are installed and detected and rebuild to use this functionality.\n" ); fprintf( stderr, "MPEG streaming is disabled.\nYou should ensure the ffmpeg libraries are installed and detected and rebuild to use this functionality.\n" );
logTerm();
zmDbClose();
return( -1 ); return( -1 );
#endif // HAVE_LIBAVCODEC #endif // HAVE_LIBAVCODEC
} }
stream.runStream(); stream.runStream();
} }
logTerm();
zmDbClose();
return( 0 ); return( 0 );
} }

View File

@ -169,6 +169,8 @@ int main(int argc, char** argv) {
stream.setStreamBitrate(bitrate); // default = 100000 (bitrate) stream.setStreamBitrate(bitrate); // default = 100000 (bitrate)
#else #else
fprintf(stderr, "MPEG streaming is disabled.\nYou should configure with the --with-ffmpeg option and rebuild to use this functionality.\n"); fprintf(stderr, "MPEG streaming is disabled.\nYou should configure with the --with-ffmpeg option and rebuild to use this functionality.\n");
logTerm();
zmDbClose();
return EXIT_FAILURE; return EXIT_FAILURE;
#endif #endif
} }
@ -192,5 +194,8 @@ int main(int argc, char** argv) {
} }
if (debug) printf("Done.\n"); if (debug) printf("Done.\n");
logTerm();
zmDbClose();
return (EXIT_SUCCESS); return (EXIT_SUCCESS);
} }

View File

@ -754,5 +754,8 @@ int main( int argc, char *argv[] )
} }
delete user; delete user;
logTerm();
zmDbClose();
return( 0 ); return( 0 );
} }