diff --git a/trunk/src/app/srs_app_caster_flv.cpp b/trunk/src/app/srs_app_caster_flv.cpp index a1b8f6f406..f5dfb698c3 100644 --- a/trunk/src/app/srs_app_caster_flv.cpp +++ b/trunk/src/app/srs_app_caster_flv.cpp @@ -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); diff --git a/trunk/src/app/srs_app_dvr.cpp b/trunk/src/app/srs_app_dvr.cpp index e2d1716dac..98c4a190b2 100644 --- a/trunk/src/app/srs_app_dvr.cpp +++ b/trunk/src/app/srs_app_dvr.cpp @@ -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(); @@ -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) { diff --git a/trunk/src/app/srs_app_hds.cpp b/trunk/src/app/srs_app_hds.cpp index de0762991d..a7c7050756 100644 --- a/trunk/src/app/srs_app_hds.cpp +++ b/trunk/src/app/srs_app_hds.cpp @@ -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; diff --git a/trunk/src/app/srs_app_http_conn.cpp b/trunk/src/app/srs_app_http_conn.cpp index 1a378dd764..3e1a230db8 100644 --- a/trunk/src/app/srs_app_http_conn.cpp +++ b/trunk/src/app/srs_app_http_conn.cpp @@ -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()) { diff --git a/trunk/src/app/srs_app_http_hooks.cpp b/trunk/src/app/srs_app_http_hooks.cpp index af7e92865d..d58d4c8b05 100644 --- a/trunk/src/app/srs_app_http_hooks.cpp +++ b/trunk/src/app/srs_app_http_hooks.cpp @@ -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(); diff --git a/trunk/src/app/srs_app_http_static.cpp b/trunk/src/app/srs_app_http_static.cpp index 01c94405fe..8faeb995a2 100644 --- a/trunk/src/app/srs_app_http_static.cpp +++ b/trunk/src/app/srs_app_http_static.cpp @@ -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; } diff --git a/trunk/src/app/srs_app_mpegts_udp.cpp b/trunk/src/app/srs_app_mpegts_udp.cpp index c9d5f342b7..2b9cdf8a2a 100644 --- a/trunk/src/app/srs_app_mpegts_udp.cpp +++ b/trunk/src/app/srs_app_mpegts_udp.cpp @@ -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; } diff --git a/trunk/src/core/srs_core_autofree.hpp b/trunk/src/core/srs_core_autofree.hpp index 9687cce7de..90a9e6b3d7 100644 --- a/trunk/src/core/srs_core_autofree.hpp +++ b/trunk/src/core/srs_core_autofree.hpp @@ -31,28 +31,41 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #include /** -* 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 _auto_free_##instance(&instance) +impl__SrsAutoFree _auto_free_##instance(&instance, false) +#define SrsAutoFreeA(className, instance) \ +impl__SrsAutoFree _auto_free_array_##instance(&instance, true) template 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() { @@ -60,7 +73,11 @@ class impl__SrsAutoFree return; } - delete *ptr; + if (is_array) { + delete[] *ptr; + } else { + delete *ptr; + } *ptr = NULL; } diff --git a/trunk/src/kernel/srs_kernel_codec.cpp b/trunk/src/kernel/srs_kernel_codec.cpp index 5c557209bf..ebdf04b4e6 100644 --- a/trunk/src/kernel/srs_kernel_codec.cpp +++ b/trunk/src/kernel/srs_kernel_codec.cpp @@ -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()) { diff --git a/trunk/src/kernel/srs_kernel_ts.cpp b/trunk/src/kernel/srs_kernel_ts.cpp index c9d795a58d..c35354620c 100644 --- a/trunk/src/kernel/srs_kernel_ts.cpp +++ b/trunk/src/kernel/srs_kernel_ts.cpp @@ -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(); @@ -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(); @@ -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(); diff --git a/trunk/src/protocol/srs_raw_avc.cpp b/trunk/src/protocol/srs_raw_avc.cpp index 882b9a6065..654959fbf8 100644 --- a/trunk/src/protocol/srs_raw_avc.cpp +++ b/trunk/src/protocol/srs_raw_avc.cpp @@ -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; @@ -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; diff --git a/trunk/src/protocol/srs_rtmp_handshake.cpp b/trunk/src/protocol/srs_rtmp_handshake.cpp index 3d94c7d70b..976b70cc2b 100644 --- a/trunk/src/protocol/srs_rtmp_handshake.cpp +++ b/trunk/src/protocol/srs_rtmp_handshake.cpp @@ -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); @@ -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); @@ -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."); @@ -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); @@ -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; } @@ -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; } diff --git a/trunk/src/protocol/srs_rtsp_stack.cpp b/trunk/src/protocol/srs_rtsp_stack.cpp index 5b691607f7..5ec3296c53 100644 --- a/trunk/src/protocol/srs_rtsp_stack.cpp +++ b/trunk/src/protocol/srs_rtsp_stack.cpp @@ -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); @@ -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) {