Skip to content

Commit

Permalink
fix the memory leak error
Browse files Browse the repository at this point in the history
  • Loading branch information
winlinvip committed Nov 28, 2013
1 parent 005f821 commit af5e7f0
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 24 deletions.
4 changes: 2 additions & 2 deletions trunk/conf/srs.conf
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion trunk/src/core/srs_core_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion trunk/src/core/srs_core_codec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
31 changes: 22 additions & 9 deletions trunk/src/core/srs_core_protocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
Expand Down
9 changes: 8 additions & 1 deletion trunk/src/core/srs_core_protocol.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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:
/**
Expand Down
12 changes: 2 additions & 10 deletions trunk/src/core/srs_core_source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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<SrsConsumer*>::iterator it;
for (it = consumers.begin(); it != consumers.end(); ++it) {
Expand Down Expand Up @@ -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;
}
Expand All @@ -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<SrsConsumer*>::iterator it;
for (it = consumers.begin(); it != consumers.end(); ++it) {
Expand Down

0 comments on commit af5e7f0

Please sign in to comment.