Skip to content

Commit

Permalink
for #515, use SrsAutoFreeA to free the array.
Browse files Browse the repository at this point in the history
  • Loading branch information
winlinvip committed Nov 2, 2015
1 parent 2af7749 commit f4f76cd
Show file tree
Hide file tree
Showing 13 changed files with 53 additions and 36 deletions.
2 changes: 1 addition & 1 deletion trunk/src/app/srs_app_caster_flv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ int SrsDynamicHttpConn::proxy(ISrsHttpResponseWriter* w, ISrsHttpMessage* r, std
srs_trace("flv: proxy %s to %s", r->uri().c_str(), output.c_str());

char* buffer = new char[SRS_HTTP_FLV_STREAM_BUFFER];
SrsAutoFree(char, buffer);
SrsAutoFreeA(char, buffer);

ISrsHttpResponseReader* rr = r->body_reader();
SrsHttpFileReader reader(rr);
Expand Down
4 changes: 2 additions & 2 deletions trunk/src/app/srs_app_dvr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ int SrsFlvSegment::write_metadata(SrsSharedPtrMessage* metadata)

int size = name->total_size() + obj->total_size();
char* payload = new char[size];
SrsAutoFree(char, payload);
SrsAutoFreeA(char, payload);

// 11B flv header, 3B object EOF, 8B number value, 1B number flag.
duration_offset = fs->tellg() + size + 11 - SrsAmf0Size::object_eof() - SrsAmf0Size::number();
Expand Down Expand Up @@ -355,7 +355,7 @@ int SrsFlvSegment::update_flv_metadata()

// buffer to write the size.
char* buf = new char[SrsAmf0Size::number()];
SrsAutoFree(char, buf);
SrsAutoFreeA(char, buf);

SrsStream stream;
if ((ret = stream.initialize(buf, SrsAmf0Size::number())) != ERROR_SUCCESS) {
Expand Down
2 changes: 1 addition & 1 deletion trunk/src/app/srs_app_hds.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ int SrsHds::flush_bootstrap()
int size = 1024*100;

char *start_abst = new char[1024*100];
SrsAutoFree(char, start_abst);
SrsAutoFreeA(char, start_abst);

int size_abst = 0;
char *start_asrt = NULL;
Expand Down
2 changes: 1 addition & 1 deletion trunk/src/app/srs_app_http_conn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -734,7 +734,7 @@ int SrsHttpMessage::body_read_all(string& body)

// cache to read.
char* buf = new char[SRS_HTTP_READ_CACHE_BYTES];
SrsAutoFree(char, buf);
SrsAutoFreeA(char, buf);

// whatever, read util EOF.
while (!_body->eof()) {
Expand Down
2 changes: 1 addition & 1 deletion trunk/src/app/srs_app_http_hooks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ int SrsHttpHooks::on_hls_notify(int cid, std::string url, SrsRequest* req, std::

int nb_buf = srs_min(nb_notify, SRS_HTTP_READ_BUFFER);
char* buf = new char[nb_buf];
SrsAutoFree(char, buf);
SrsAutoFreeA(char, buf);

int nb_read = 0;
ISrsHttpResponseReader* br = msg->body_reader();
Expand Down
2 changes: 1 addition & 1 deletion trunk/src/app/srs_app_http_static.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ int SrsVodStream::serve_flv_stream(ISrsHttpResponseWriter* w, ISrsHttpMessage* r
}
}
sh_data = new char[sh_size];
SrsAutoFree(char, sh_data);
SrsAutoFreeA(char, sh_data);
if ((ret = fs.read(sh_data, sh_size, NULL)) != ERROR_SUCCESS) {
return ret;
}
Expand Down
2 changes: 1 addition & 1 deletion trunk/src/app/srs_app_mpegts_udp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ int SrsMpegtsOverUdp::on_udp_bytes(string host, int port, char* buf, int nb_buf)
buffer->erase(buffer->length());
int nb_fbuf = fr.filesize();
char* fbuf = new char[nb_fbuf];
SrsAutoFree(char, fbuf);
SrsAutoFreeA(char, fbuf);
if ((ret = fr.read(fbuf, nb_fbuf, NULL)) != ERROR_SUCCESS) {
return ret;
}
Expand Down
45 changes: 31 additions & 14 deletions trunk/src/core/srs_core_autofree.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,36 +31,53 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#include <srs_core.hpp>

/**
* auto free the instance in the current scope, for instance, MyClass* ptr,
* which is a ptr and this class will:
* 1. free the ptr.
* 2. set ptr to NULL.
* Usage:
* MyClass* po = new MyClass();
* // ...... use po
* SrsAutoFree(MyClass, po);
*/
* auto free the instance in the current scope, for instance, MyClass* ptr,
* which is a ptr and this class will:
* 1. free the ptr.
* 2. set ptr to NULL.
*
* Usage:
* MyClass* po = new MyClass();
* // ...... use po
* SrsAutoFree(MyClass, po);
*
* Usage for array:
* MyClass** pa = new MyClass*[size];
* // ....... use pa
* SrsAutoFreeA(MyClass*, pa);
*
* @remark the MyClass can be basic type, for instance, SrsAutoFreeA(char, pstr),
* where the char* pstr = new char[size].
*/
#define SrsAutoFree(className, instance) \
impl__SrsAutoFree<className> _auto_free_##instance(&instance)
impl__SrsAutoFree<className> _auto_free_##instance(&instance, false)
#define SrsAutoFreeA(className, instance) \
impl__SrsAutoFree<className> _auto_free_array_##instance(&instance, true)
template<class T>
class impl__SrsAutoFree
{
private:
T** ptr;
bool is_array;
public:
/**
* auto delete the ptr.
*/
impl__SrsAutoFree(T** p) {
* auto delete the ptr.
*/
impl__SrsAutoFree(T** p, bool array) {
ptr = p;
is_array = array;
}

virtual ~impl__SrsAutoFree() {
if (ptr == NULL || *ptr == NULL) {
return;
}

delete *ptr;
if (is_array) {
delete[] *ptr;
} else {
delete *ptr;
}

*ptr = NULL;
}
Expand Down
2 changes: 1 addition & 1 deletion trunk/src/kernel/srs_kernel_codec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -914,7 +914,7 @@ int SrsAvcAacCodec::avc_demux_sps()
// decode the rbsp from sps.
// rbsp[ i ] a raw byte sequence payload is specified as an ordered sequence of bytes.
int8_t* rbsp = new int8_t[sequenceParameterSetLength];
SrsAutoFree(int8_t, rbsp);
SrsAutoFreeA(int8_t, rbsp);

int nb_rbsp = 0;
while (!stream.empty()) {
Expand Down
6 changes: 3 additions & 3 deletions trunk/src/kernel/srs_kernel_ts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ int SrsTsContext::encode_pat_pmt(SrsFileWriter* writer, int16_t vpid, SrsTsStrea
SrsAutoFree(SrsTsPacket, pkt);

char* buf = new char[SRS_TS_PACKET_SIZE];
SrsAutoFree(char, buf);
SrsAutoFreeA(char, buf);

// set the left bytes with 0xFF.
int nb_buf = pkt->size();
Expand All @@ -412,7 +412,7 @@ int SrsTsContext::encode_pat_pmt(SrsFileWriter* writer, int16_t vpid, SrsTsStrea
SrsAutoFree(SrsTsPacket, pkt);

char* buf = new char[SRS_TS_PACKET_SIZE];
SrsAutoFree(char, buf);
SrsAutoFreeA(char, buf);

// set the left bytes with 0xFF.
int nb_buf = pkt->size();
Expand Down Expand Up @@ -485,7 +485,7 @@ int SrsTsContext::encode_pes(SrsFileWriter* writer, SrsTsMessage* msg, int16_t p
SrsAutoFree(SrsTsPacket, pkt);

char* buf = new char[SRS_TS_PACKET_SIZE];
SrsAutoFree(char, buf);
SrsAutoFreeA(char, buf);

// set the left bytes with 0xFF.
int nb_buf = pkt->size();
Expand Down
4 changes: 2 additions & 2 deletions trunk/src/protocol/srs_raw_avc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ int SrsRawH264Stream::mux_sequence_header(string sps, string pps, u_int32_t dts,
+ 3 + (int)sps.length()
+ 3 + (int)pps.length();
char* packet = new char[nb_packet];
SrsAutoFree(char, packet);
SrsAutoFreeA(char, packet);

// use stream to generate the h264 packet.
SrsStream stream;
Expand Down Expand Up @@ -236,7 +236,7 @@ int SrsRawH264Stream::mux_ipb_frame(char* frame, int nb_frame, string& ibp)
// NALUnit
int nb_packet = 4 + nb_frame;
char* packet = new char[nb_packet];
SrsAutoFree(char, packet);
SrsAutoFreeA(char, packet);

// use stream to generate the h264 packet.
SrsStream stream;
Expand Down
12 changes: 6 additions & 6 deletions trunk/src/protocol/srs_rtmp_handshake.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ namespace _srs_internal
}

srs_assert(c1_digest != NULL);
SrsAutoFree(char, c1_digest);
SrsAutoFreeA(char, c1_digest);

memcpy(digest.digest, c1_digest, 32);

Expand All @@ -506,7 +506,7 @@ namespace _srs_internal
}

srs_assert(c1_digest != NULL);
SrsAutoFree(char, c1_digest);
SrsAutoFreeA(char, c1_digest);

is_valid = srs_bytes_equals(digest.digest, c1_digest, 32);

Expand Down Expand Up @@ -546,7 +546,7 @@ namespace _srs_internal
srs_verbose("calc s1 digest success.");

srs_assert(s1_digest != NULL);
SrsAutoFree(char, s1_digest);
SrsAutoFreeA(char, s1_digest);

memcpy(digest.digest, s1_digest, 32);
srs_verbose("copy s1 key success.");
Expand All @@ -566,7 +566,7 @@ namespace _srs_internal
}

srs_assert(s1_digest != NULL);
SrsAutoFree(char, s1_digest);
SrsAutoFreeA(char, s1_digest);

is_valid = srs_bytes_equals(digest.digest, s1_digest, 32);

Expand All @@ -585,7 +585,7 @@ namespace _srs_internal
* @return a new allocated bytes, user must free it.
*/
char* c1s1_joined_bytes = new char[1536 -32];
SrsAutoFree(char, c1s1_joined_bytes);
SrsAutoFreeA(char, c1s1_joined_bytes);
if ((ret = copy_to(owner, c1s1_joined_bytes, 1536 - 32, false)) != ERROR_SUCCESS) {
return ret;
}
Expand Down Expand Up @@ -613,7 +613,7 @@ namespace _srs_internal
* @return a new allocated bytes, user must free it.
*/
char* c1s1_joined_bytes = new char[1536 -32];
SrsAutoFree(char, c1s1_joined_bytes);
SrsAutoFreeA(char, c1s1_joined_bytes);
if ((ret = copy_to(owner, c1s1_joined_bytes, 1536 - 32, false)) != ERROR_SUCCESS) {
return ret;
}
Expand Down
4 changes: 2 additions & 2 deletions trunk/src/protocol/srs_rtsp_stack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,7 @@ int SrsRtspSdp::parse_fmtp_attribute(string attr)
}

char* tmp_sh = new char[item_value.length()];
SrsAutoFree(char, tmp_sh);
SrsAutoFreeA(char, tmp_sh);
int nb_tmp_sh = ff_hex_to_data((u_int8_t*)tmp_sh, item_value.c_str());
srs_assert(nb_tmp_sh > 0);
audio_sh.append(tmp_sh, nb_tmp_sh);
Expand Down Expand Up @@ -603,7 +603,7 @@ string SrsRtspSdp::base64_decode(string value)

int nb_output = (int)(value.length() * 2);
u_int8_t* output = new u_int8_t[nb_output];
SrsAutoFree(u_int8_t, output);
SrsAutoFreeA(u_int8_t, output);

int ret = srs_av_base64_decode(output, (char*)value.c_str(), nb_output);
if (ret <= 0) {
Expand Down

0 comments on commit f4f76cd

Please # to comment.