From af5e7f002fc2d3aa98db470c60318874d25d9d17 Mon Sep 17 00:00:00 2001 From: winlin Date: Thu, 28 Nov 2013 14:53:46 +0800 Subject: [PATCH] fix the memory leak error --- trunk/conf/srs.conf | 4 ++-- trunk/src/core/srs_core_client.cpp | 2 +- trunk/src/core/srs_core_codec.cpp | 2 +- trunk/src/core/srs_core_protocol.cpp | 31 ++++++++++++++++++++-------- trunk/src/core/srs_core_protocol.hpp | 9 +++++++- trunk/src/core/srs_core_source.cpp | 12 ++--------- 6 files changed, 36 insertions(+), 24 deletions(-) diff --git a/trunk/conf/srs.conf b/trunk/conf/srs.conf index b8a84849ed..2b549884e4 100755 --- a/trunk/conf/srs.conf +++ b/trunk/conf/srs.conf @@ -1,5 +1,5 @@ # the listen ports, split by space. -listen 1935; +listen 1936; # the default chunk size is 128, max is 65536, # some client does not support chunk size change, # however, most clients supports it and it can improve @@ -10,7 +10,7 @@ chunk_size 65000; # for which cannot identify the required vhost. vhost __defaultVhost__ { enabled on; - gop_cache on; + gop_cache off; hls on; hls_path ./objs/nginx/html; hls_fragment 5; diff --git a/trunk/src/core/srs_core_client.cpp b/trunk/src/core/srs_core_client.cpp index d066128f47..5ca00bfe8e 100644 --- a/trunk/src/core/srs_core_client.cpp +++ b/trunk/src/core/srs_core_client.cpp @@ -384,7 +384,7 @@ int SrsClient::process_publish_message(SrsSource* source, SrsCommonMessage* msg, int ret = ERROR_SUCCESS; // process audio packet - if (msg->header.is_audio()) { + if (false && msg->header.is_audio()) { if ((ret = source->on_audio(msg)) != ERROR_SUCCESS) { srs_error("source process audio message failed. ret=%d", ret); return ret; diff --git a/trunk/src/core/srs_core_codec.cpp b/trunk/src/core/srs_core_codec.cpp index 24faa93762..af1b915190 100644 --- a/trunk/src/core/srs_core_codec.cpp +++ b/trunk/src/core/srs_core_codec.cpp @@ -41,7 +41,7 @@ void SrsCodecBuffer::append(void* data, int len) { srs_assert(data); srs_assert(len > 0); - + bytes = (char*)realloc(bytes, size + len); memcpy(bytes + size, data, len); size += len; diff --git a/trunk/src/core/srs_core_protocol.cpp b/trunk/src/core/srs_core_protocol.cpp index 48064698e3..c6c490277d 100644 --- a/trunk/src/core/srs_core_protocol.cpp +++ b/trunk/src/core/srs_core_protocol.cpp @@ -1356,11 +1356,26 @@ bool SrsSharedPtrMessage::can_decode() return false; } -int SrsSharedPtrMessage::initialize(ISrsMessage* msg, char* payload, int size) +int SrsSharedPtrMessage::initialize(SrsCommonMessage* source) { int ret = ERROR_SUCCESS; - srs_assert(msg != NULL); + if ((ret = initialize(source, (char*)source->payload, source->size)) != ERROR_SUCCESS) { + return ret; + } + + // detach the payload from source + source->payload = NULL; + source->size = 0; + + return ret; +} + +int SrsSharedPtrMessage::initialize(SrsCommonMessage* source, char* payload, int size) +{ + int ret = ERROR_SUCCESS; + + srs_assert(source != NULL); if (ptr) { ret = ERROR_SYSTEM_ASSERT_FAILED; srs_error("should not set the payload twice. ret=%d", ret); @@ -1369,20 +1384,18 @@ int SrsSharedPtrMessage::initialize(ISrsMessage* msg, char* payload, int size) return ret; } - header = msg->header; + header = source->header; header.payload_length = size; ptr = new SrsSharedPtr(); - // should copy the payload once - // TODO: maybe can directly attach the common message. - ptr->payload = new char[size]; - memcpy(ptr->payload, payload, size); + // direct attach the data of common message. + ptr->payload = payload; ptr->size = size; - if (msg->header.is_video()) { + if (source->header.is_video()) { ptr->perfer_cid = RTMP_CID_Video; - } else if (msg->header.is_audio()) { + } else if (source->header.is_audio()) { ptr->perfer_cid = RTMP_CID_Audio; } else { ptr->perfer_cid = RTMP_CID_OverConnection2; diff --git a/trunk/src/core/srs_core_protocol.hpp b/trunk/src/core/srs_core_protocol.hpp index 8c60440038..cc12332f67 100644 --- a/trunk/src/core/srs_core_protocol.hpp +++ b/trunk/src/core/srs_core_protocol.hpp @@ -375,8 +375,15 @@ class SrsSharedPtrMessage : public ISrsMessage public: /** * set the shared payload. + * we will detach the payload of source, + * so ensure donot use it before. */ - virtual int initialize(ISrsMessage* msg, char* payload, int size); + virtual int initialize(SrsCommonMessage* source); + /** + * set the shared payload. + * we will use the payload, donot use the payload of source. + */ + virtual int initialize(SrsCommonMessage* source, char* payload, int size); virtual SrsSharedPtrMessage* copy(); public: /** diff --git a/trunk/src/core/srs_core_source.cpp b/trunk/src/core/srs_core_source.cpp index e5d61ec765..cece1cd3c3 100644 --- a/trunk/src/core/srs_core_source.cpp +++ b/trunk/src/core/srs_core_source.cpp @@ -370,7 +370,7 @@ int SrsSource::on_audio(SrsCommonMessage* audio) SrsSharedPtrMessage* msg = new SrsSharedPtrMessage(); SrsAutoFree(SrsSharedPtrMessage, msg, false); - if ((ret = msg->initialize(audio, (char*)audio->payload, audio->size)) != ERROR_SUCCESS) { + if ((ret = msg->initialize(audio)) != ERROR_SUCCESS) { srs_error("initialize the audio failed. ret=%d", ret); return ret; } @@ -383,10 +383,6 @@ int SrsSource::on_audio(SrsCommonMessage* audio) } #endif - // detach the original audio - audio->payload = NULL; - audio->size = 0; - // copy to all consumer std::vector::iterator it; for (it = consumers.begin(); it != consumers.end(); ++it) { @@ -422,7 +418,7 @@ int SrsSource::on_video(SrsCommonMessage* video) SrsSharedPtrMessage* msg = new SrsSharedPtrMessage(); SrsAutoFree(SrsSharedPtrMessage, msg, false); - if ((ret = msg->initialize(video, (char*)video->payload, video->size)) != ERROR_SUCCESS) { + if ((ret = msg->initialize(video)) != ERROR_SUCCESS) { srs_error("initialize the video failed. ret=%d", ret); return ret; } @@ -435,10 +431,6 @@ int SrsSource::on_video(SrsCommonMessage* video) } #endif - // detach the original audio - video->payload = NULL; - video->size = 0; - // copy to all consumer std::vector::iterator it; for (it = consumers.begin(); it != consumers.end(); ++it) {