Skip to content

Commit

Permalink
SmartPtr: Support detect memory leak by valgrind.
Browse files Browse the repository at this point in the history
  • Loading branch information
winlinvip committed Jun 21, 2024
1 parent 1f9309a commit 9476ab2
Show file tree
Hide file tree
Showing 8 changed files with 170 additions and 18 deletions.
2 changes: 1 addition & 1 deletion trunk/src/app/srs_app_conn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ SrsResourceManager::~SrsResourceManager()
trd->stop();

srs_freep(trd);
srs_cond_destroy(cond);
}
srs_cond_destroy(cond);

clear();

Expand Down
2 changes: 1 addition & 1 deletion trunk/src/app/srs_app_hourglass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ void SrsFastTimer::unsubscribe(ISrsFastTimer* timer)
{
vector<ISrsFastTimer*>::iterator it = std::find(handlers_.begin(), handlers_.end(), timer);
if (it != handlers_.end()) {
it = handlers_.erase(it);
handlers_.erase(it);
}
}

Expand Down
17 changes: 8 additions & 9 deletions trunk/src/app/srs_app_hybrid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,19 +135,20 @@ SrsHybridServer::SrsHybridServer()

SrsHybridServer::~SrsHybridServer()
{
srs_freep(clock_monitor_);

srs_freep(timer20ms_);
srs_freep(timer100ms_);
srs_freep(timer1s_);
srs_freep(timer5s_);

// We must free servers first, because it may depend on the timers of hybrid server.
vector<ISrsHybridServer*>::iterator it;
for (it = servers.begin(); it != servers.end(); ++it) {
ISrsHybridServer* server = *it;
srs_freep(server);
}
servers.clear();

srs_freep(clock_monitor_);

srs_freep(timer20ms_);
srs_freep(timer100ms_);
srs_freep(timer1s_);
srs_freep(timer5s_);
}

void SrsHybridServer::register_server(ISrsHybridServer* svr)
Expand Down Expand Up @@ -237,8 +238,6 @@ void SrsHybridServer::stop()
ISrsHybridServer* server = *it;
server->stop();
}

srs_st_destroy();
}

SrsServerAdapter* SrsHybridServer::srs()
Expand Down
9 changes: 3 additions & 6 deletions trunk/src/app/srs_app_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ using namespace std;
#include <srs_protocol_log.hpp>
#include <srs_app_latest_version.hpp>
#include <srs_app_conn.hpp>
#include <srs_app_threads.hpp>
#ifdef SRS_RTC
#include <srs_app_rtc_network.hpp>
#include <srs_app_rtc_server.hpp>
Expand Down Expand Up @@ -370,8 +371,6 @@ SrsServer::~SrsServer()

void SrsServer::destroy()
{
srs_warn("start destroy server");

srs_freep(trd_);
srs_freep(timer_);

Expand Down Expand Up @@ -869,11 +868,8 @@ void SrsServer::stop()
srs_trace("srs gracefully quit");
}

// This is the last line log of SRS.
srs_trace("srs terminated");

// for valgrind to detect.
srs_freep(_srs_config);
srs_freep(_srs_log);
}

srs_error_t SrsServer::cycle()
Expand Down Expand Up @@ -1406,6 +1402,7 @@ srs_error_t SrsServerAdapter::run(SrsWaitGroup* wg)

void SrsServerAdapter::stop()
{
srs->stop();
}

SrsServer* SrsServerAdapter::instance()
Expand Down
148 changes: 148 additions & 0 deletions trunk/src/app/srs_app_threads.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,154 @@ srs_error_t srs_global_initialize()
return err;
}

void srs_global_dispose()
{
// Note that hybrid depends on sources.
srs_freep(_srs_hybrid);
srs_freep(_srs_sources);

srs_freep(_srs_clock);

srs_freep(_srs_stages);
srs_freep(_srs_circuit_breaker);

#ifdef SRS_SRT
srs_freep(_srs_srt_sources);
#endif

#ifdef SRS_RTC
srs_freep(_srs_rtc_sources);
srs_freep(_srs_blackhole);
srs_freep(_srs_rtc_manager);
srs_freep(_srs_rtc_dtls_certificate);
#endif
#ifdef SRS_GB28181
srs_freep(_srs_gb_manager);
#endif

srs_freep(_srs_pps_ids);
srs_freep(_srs_pps_fids);
srs_freep(_srs_pps_fids_level0);
srs_freep(_srs_pps_dispose);

srs_freep(_srs_pps_timer);
srs_freep(_srs_pps_conn);
srs_freep(_srs_pps_pub);

#ifdef SRS_RTC
srs_freep(_srs_pps_snack);
srs_freep(_srs_pps_snack2);
srs_freep(_srs_pps_snack3);
srs_freep(_srs_pps_snack4);
srs_freep(_srs_pps_sanack);
srs_freep(_srs_pps_svnack);

srs_freep(_srs_pps_rnack);
srs_freep(_srs_pps_rnack2);
srs_freep(_srs_pps_rhnack);
srs_freep(_srs_pps_rmnack);
#endif

#if defined(SRS_DEBUG) && defined(SRS_DEBUG_STATS)
srs_freep(_srs_pps_recvfrom);
srs_freep(_srs_pps_recvfrom_eagain);
srs_freep(_srs_pps_sendto);
srs_freep(_srs_pps_sendto_eagain);

srs_freep(_srs_pps_read);
srs_freep(_srs_pps_read_eagain);
srs_freep(_srs_pps_readv);
srs_freep(_srs_pps_readv_eagain);
srs_freep(_srs_pps_writev);
srs_freep(_srs_pps_writev_eagain);

srs_freep(_srs_pps_recvmsg);
srs_freep(_srs_pps_recvmsg_eagain);
srs_freep(_srs_pps_sendmsg);
srs_freep(_srs_pps_sendmsg_eagain);

srs_freep(_srs_pps_epoll);
srs_freep(_srs_pps_epoll_zero);
srs_freep(_srs_pps_epoll_shake);
srs_freep(_srs_pps_epoll_spin);

srs_freep(_srs_pps_sched_15ms);
srs_freep(_srs_pps_sched_20ms);
srs_freep(_srs_pps_sched_25ms);
srs_freep(_srs_pps_sched_30ms);
srs_freep(_srs_pps_sched_35ms);
srs_freep(_srs_pps_sched_40ms);
srs_freep(_srs_pps_sched_80ms);
srs_freep(_srs_pps_sched_160ms);
srs_freep(_srs_pps_sched_s);
#endif

srs_freep(_srs_pps_clock_15ms);
srs_freep(_srs_pps_clock_20ms);
srs_freep(_srs_pps_clock_25ms);
srs_freep(_srs_pps_clock_30ms);
srs_freep(_srs_pps_clock_35ms);
srs_freep(_srs_pps_clock_40ms);
srs_freep(_srs_pps_clock_80ms);
srs_freep(_srs_pps_clock_160ms);
srs_freep(_srs_pps_timer_s);

#if defined(SRS_DEBUG) && defined(SRS_DEBUG_STATS)
srs_freep(_srs_pps_thread_run);
srs_freep(_srs_pps_thread_idle);
srs_freep(_srs_pps_thread_yield);
srs_freep(_srs_pps_thread_yield2);
#endif

srs_freep(_srs_pps_rpkts);
srs_freep(_srs_pps_addrs);
srs_freep(_srs_pps_fast_addrs);

srs_freep(_srs_pps_spkts);
srs_freep(_srs_pps_objs_msgs);

#ifdef SRS_RTC
srs_freep(_srs_pps_sstuns);
srs_freep(_srs_pps_srtcps);
srs_freep(_srs_pps_srtps);

srs_freep(_srs_pps_rstuns);
srs_freep(_srs_pps_rrtps);
srs_freep(_srs_pps_rrtcps);

srs_freep(_srs_pps_aloss2);

srs_freep(_srs_pps_pli);
srs_freep(_srs_pps_twcc);
srs_freep(_srs_pps_rr);

srs_freep(_srs_pps_objs_rtps);
srs_freep(_srs_pps_objs_rraw);
srs_freep(_srs_pps_objs_rfua);
srs_freep(_srs_pps_objs_rbuf);
srs_freep(_srs_pps_objs_rothers);
#endif

srs_freep(_srs_dvr_async);

#ifdef SRS_APM
srs_freep(_srs_cls);
srs_freep(_srs_apm);
#endif

srs_freep(_srs_reload_err);

// Note that we never free the logging, because it's used after thread terminated.
//srs_freep(_srs_log);
//srs_freep(_srs_config);
//srs_freep(_srs_context);
//srs_freep(_srs_pps_cids_get);
//srs_freep(_srs_pps_cids_set);

// Dispose ST finally, which may be used by other global objects.
srs_st_destroy();
}

SrsThreadMutex::SrsThreadMutex()
{
// https://man7.org/linux/man-pages/man3/pthread_mutexattr_init.3.html
Expand Down
1 change: 1 addition & 0 deletions trunk/src/app/srs_app_threads.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ extern SrsCircuitBreaker* _srs_circuit_breaker;

// Initialize global shared variables cross all threads.
extern srs_error_t srs_global_initialize();
extern void srs_global_dispose();

// The thread mutex wrapper, without error.
class SrsThreadMutex
Expand Down
2 changes: 1 addition & 1 deletion trunk/src/kernel/srs_kernel_error.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ class SrsCplxError
};

// Error helpers, should use these functions to new or wrap an error.
#define srs_success 0 // SrsCplxError::success()
#define srs_success NULL // SrsCplxError::success()
#define srs_error_new(ret, fmt, ...) SrsCplxError::create(__FUNCTION__, __FILE__, __LINE__, ret, fmt, ##__VA_ARGS__)
#define srs_error_wrap(err, fmt, ...) SrsCplxError::wrap(__FUNCTION__, __FILE__, __LINE__, err, fmt, ##__VA_ARGS__)
#define srs_error_copy(err) SrsCplxError::copy(err)
Expand Down
7 changes: 7 additions & 0 deletions trunk/src/main/srs_main_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ extern void srs_free_global_system_ips();
extern void asan_report_callback(const char* str);
#endif

extern SrsPps* _srs_pps_cids_get;
extern SrsPps* _srs_pps_cids_set;

/**
* main entrance.
*/
Expand Down Expand Up @@ -522,6 +525,10 @@ srs_error_t run_hybrid_server(void* /*arg*/)
// After all done, stop and cleanup.
_srs_hybrid->stop();

// Dispose all global objects, note that we should do this in the hybrid thread, because it may
// depend on the ST when disposing.
srs_global_dispose();

return err;
}

0 comments on commit 9476ab2

Please sign in to comment.