winlin

for #515, use SrsAutoFreeA to free the array.

@@ -148,7 +148,7 @@ int SrsDynamicHttpConn::proxy(ISrsHttpResponseWriter* w, ISrsHttpMessage* r, std @@ -148,7 +148,7 @@ int SrsDynamicHttpConn::proxy(ISrsHttpResponseWriter* w, ISrsHttpMessage* r, std
148 srs_trace("flv: proxy %s to %s", r->uri().c_str(), output.c_str()); 148 srs_trace("flv: proxy %s to %s", r->uri().c_str(), output.c_str());
149 149
150 char* buffer = new char[SRS_HTTP_FLV_STREAM_BUFFER]; 150 char* buffer = new char[SRS_HTTP_FLV_STREAM_BUFFER];
151 - SrsAutoFree(char, buffer); 151 + SrsAutoFreeA(char, buffer);
152 152
153 ISrsHttpResponseReader* rr = r->body_reader(); 153 ISrsHttpResponseReader* rr = r->body_reader();
154 SrsHttpFileReader reader(rr); 154 SrsHttpFileReader reader(rr);
@@ -240,7 +240,7 @@ int SrsFlvSegment::write_metadata(SrsSharedPtrMessage* metadata) @@ -240,7 +240,7 @@ int SrsFlvSegment::write_metadata(SrsSharedPtrMessage* metadata)
240 240
241 int size = name->total_size() + obj->total_size(); 241 int size = name->total_size() + obj->total_size();
242 char* payload = new char[size]; 242 char* payload = new char[size];
243 - SrsAutoFree(char, payload); 243 + SrsAutoFreeA(char, payload);
244 244
245 // 11B flv header, 3B object EOF, 8B number value, 1B number flag. 245 // 11B flv header, 3B object EOF, 8B number value, 1B number flag.
246 duration_offset = fs->tellg() + size + 11 - SrsAmf0Size::object_eof() - SrsAmf0Size::number(); 246 duration_offset = fs->tellg() + size + 11 - SrsAmf0Size::object_eof() - SrsAmf0Size::number();
@@ -355,7 +355,7 @@ int SrsFlvSegment::update_flv_metadata() @@ -355,7 +355,7 @@ int SrsFlvSegment::update_flv_metadata()
355 355
356 // buffer to write the size. 356 // buffer to write the size.
357 char* buf = new char[SrsAmf0Size::number()]; 357 char* buf = new char[SrsAmf0Size::number()];
358 - SrsAutoFree(char, buf); 358 + SrsAutoFreeA(char, buf);
359 359
360 SrsStream stream; 360 SrsStream stream;
361 if ((ret = stream.initialize(buf, SrsAmf0Size::number())) != ERROR_SUCCESS) { 361 if ((ret = stream.initialize(buf, SrsAmf0Size::number())) != ERROR_SUCCESS) {
@@ -482,7 +482,7 @@ int SrsHds::flush_bootstrap() @@ -482,7 +482,7 @@ int SrsHds::flush_bootstrap()
482 int size = 1024*100; 482 int size = 1024*100;
483 483
484 char *start_abst = new char[1024*100]; 484 char *start_abst = new char[1024*100];
485 - SrsAutoFree(char, start_abst); 485 + SrsAutoFreeA(char, start_abst);
486 486
487 int size_abst = 0; 487 int size_abst = 0;
488 char *start_asrt = NULL; 488 char *start_asrt = NULL;
@@ -734,7 +734,7 @@ int SrsHttpMessage::body_read_all(string& body) @@ -734,7 +734,7 @@ int SrsHttpMessage::body_read_all(string& body)
734 734
735 // cache to read. 735 // cache to read.
736 char* buf = new char[SRS_HTTP_READ_CACHE_BYTES]; 736 char* buf = new char[SRS_HTTP_READ_CACHE_BYTES];
737 - SrsAutoFree(char, buf); 737 + SrsAutoFreeA(char, buf);
738 738
739 // whatever, read util EOF. 739 // whatever, read util EOF.
740 while (!_body->eof()) { 740 while (!_body->eof()) {
@@ -380,7 +380,7 @@ int SrsHttpHooks::on_hls_notify(int cid, std::string url, SrsRequest* req, std:: @@ -380,7 +380,7 @@ int SrsHttpHooks::on_hls_notify(int cid, std::string url, SrsRequest* req, std::
380 380
381 int nb_buf = srs_min(nb_notify, SRS_HTTP_READ_BUFFER); 381 int nb_buf = srs_min(nb_notify, SRS_HTTP_READ_BUFFER);
382 char* buf = new char[nb_buf]; 382 char* buf = new char[nb_buf];
383 - SrsAutoFree(char, buf); 383 + SrsAutoFreeA(char, buf);
384 384
385 int nb_read = 0; 385 int nb_read = 0;
386 ISrsHttpResponseReader* br = msg->body_reader(); 386 ISrsHttpResponseReader* br = msg->body_reader();
@@ -116,7 +116,7 @@ int SrsVodStream::serve_flv_stream(ISrsHttpResponseWriter* w, ISrsHttpMessage* r @@ -116,7 +116,7 @@ int SrsVodStream::serve_flv_stream(ISrsHttpResponseWriter* w, ISrsHttpMessage* r
116 } 116 }
117 } 117 }
118 sh_data = new char[sh_size]; 118 sh_data = new char[sh_size];
119 - SrsAutoFree(char, sh_data); 119 + SrsAutoFreeA(char, sh_data);
120 if ((ret = fs.read(sh_data, sh_size, NULL)) != ERROR_SUCCESS) { 120 if ((ret = fs.read(sh_data, sh_size, NULL)) != ERROR_SUCCESS) {
121 return ret; 121 return ret;
122 } 122 }
@@ -199,7 +199,7 @@ int SrsMpegtsOverUdp::on_udp_bytes(string host, int port, char* buf, int nb_buf) @@ -199,7 +199,7 @@ int SrsMpegtsOverUdp::on_udp_bytes(string host, int port, char* buf, int nb_buf)
199 buffer->erase(buffer->length()); 199 buffer->erase(buffer->length());
200 int nb_fbuf = fr.filesize(); 200 int nb_fbuf = fr.filesize();
201 char* fbuf = new char[nb_fbuf]; 201 char* fbuf = new char[nb_fbuf];
202 - SrsAutoFree(char, fbuf); 202 + SrsAutoFreeA(char, fbuf);
203 if ((ret = fr.read(fbuf, nb_fbuf, NULL)) != ERROR_SUCCESS) { 203 if ((ret = fr.read(fbuf, nb_fbuf, NULL)) != ERROR_SUCCESS) {
204 return ret; 204 return ret;
205 } 205 }
@@ -31,28 +31,41 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -31,28 +31,41 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 #include <srs_core.hpp> 31 #include <srs_core.hpp>
32 32
33 /** 33 /**
34 -* auto free the instance in the current scope, for instance, MyClass* ptr,  
35 -* which is a ptr and this class will:  
36 -* 1. free the ptr.  
37 -* 2. set ptr to NULL.  
38 -* Usage:  
39 -* MyClass* po = new MyClass();  
40 -* // ...... use po  
41 -* SrsAutoFree(MyClass, po);  
42 -*/ 34 + * auto free the instance in the current scope, for instance, MyClass* ptr,
  35 + * which is a ptr and this class will:
  36 + * 1. free the ptr.
  37 + * 2. set ptr to NULL.
  38 + *
  39 + * Usage:
  40 + * MyClass* po = new MyClass();
  41 + * // ...... use po
  42 + * SrsAutoFree(MyClass, po);
  43 + *
  44 + * Usage for array:
  45 + * MyClass** pa = new MyClass*[size];
  46 + * // ....... use pa
  47 + * SrsAutoFreeA(MyClass*, pa);
  48 + *
  49 + * @remark the MyClass can be basic type, for instance, SrsAutoFreeA(char, pstr),
  50 + * where the char* pstr = new char[size].
  51 + */
43 #define SrsAutoFree(className, instance) \ 52 #define SrsAutoFree(className, instance) \
44 - impl__SrsAutoFree<className> _auto_free_##instance(&instance) 53 +impl__SrsAutoFree<className> _auto_free_##instance(&instance, false)
  54 +#define SrsAutoFreeA(className, instance) \
  55 +impl__SrsAutoFree<className> _auto_free_array_##instance(&instance, true)
45 template<class T> 56 template<class T>
46 class impl__SrsAutoFree 57 class impl__SrsAutoFree
47 { 58 {
48 private: 59 private:
49 T** ptr; 60 T** ptr;
  61 + bool is_array;
50 public: 62 public:
51 /** 63 /**
52 - * auto delete the ptr.  
53 - */  
54 - impl__SrsAutoFree(T** p) { 64 + * auto delete the ptr.
  65 + */
  66 + impl__SrsAutoFree(T** p, bool array) {
55 ptr = p; 67 ptr = p;
  68 + is_array = array;
56 } 69 }
57 70
58 virtual ~impl__SrsAutoFree() { 71 virtual ~impl__SrsAutoFree() {
@@ -60,7 +73,11 @@ public: @@ -60,7 +73,11 @@ public:
60 return; 73 return;
61 } 74 }
62 75
63 - delete *ptr; 76 + if (is_array) {
  77 + delete[] *ptr;
  78 + } else {
  79 + delete *ptr;
  80 + }
64 81
65 *ptr = NULL; 82 *ptr = NULL;
66 } 83 }
@@ -914,7 +914,7 @@ int SrsAvcAacCodec::avc_demux_sps() @@ -914,7 +914,7 @@ int SrsAvcAacCodec::avc_demux_sps()
914 // decode the rbsp from sps. 914 // decode the rbsp from sps.
915 // rbsp[ i ] a raw byte sequence payload is specified as an ordered sequence of bytes. 915 // rbsp[ i ] a raw byte sequence payload is specified as an ordered sequence of bytes.
916 int8_t* rbsp = new int8_t[sequenceParameterSetLength]; 916 int8_t* rbsp = new int8_t[sequenceParameterSetLength];
917 - SrsAutoFree(int8_t, rbsp); 917 + SrsAutoFreeA(int8_t, rbsp);
918 918
919 int nb_rbsp = 0; 919 int nb_rbsp = 0;
920 while (!stream.empty()) { 920 while (!stream.empty()) {
@@ -387,7 +387,7 @@ int SrsTsContext::encode_pat_pmt(SrsFileWriter* writer, int16_t vpid, SrsTsStrea @@ -387,7 +387,7 @@ int SrsTsContext::encode_pat_pmt(SrsFileWriter* writer, int16_t vpid, SrsTsStrea
387 SrsAutoFree(SrsTsPacket, pkt); 387 SrsAutoFree(SrsTsPacket, pkt);
388 388
389 char* buf = new char[SRS_TS_PACKET_SIZE]; 389 char* buf = new char[SRS_TS_PACKET_SIZE];
390 - SrsAutoFree(char, buf); 390 + SrsAutoFreeA(char, buf);
391 391
392 // set the left bytes with 0xFF. 392 // set the left bytes with 0xFF.
393 int nb_buf = pkt->size(); 393 int nb_buf = pkt->size();
@@ -412,7 +412,7 @@ int SrsTsContext::encode_pat_pmt(SrsFileWriter* writer, int16_t vpid, SrsTsStrea @@ -412,7 +412,7 @@ int SrsTsContext::encode_pat_pmt(SrsFileWriter* writer, int16_t vpid, SrsTsStrea
412 SrsAutoFree(SrsTsPacket, pkt); 412 SrsAutoFree(SrsTsPacket, pkt);
413 413
414 char* buf = new char[SRS_TS_PACKET_SIZE]; 414 char* buf = new char[SRS_TS_PACKET_SIZE];
415 - SrsAutoFree(char, buf); 415 + SrsAutoFreeA(char, buf);
416 416
417 // set the left bytes with 0xFF. 417 // set the left bytes with 0xFF.
418 int nb_buf = pkt->size(); 418 int nb_buf = pkt->size();
@@ -485,7 +485,7 @@ int SrsTsContext::encode_pes(SrsFileWriter* writer, SrsTsMessage* msg, int16_t p @@ -485,7 +485,7 @@ int SrsTsContext::encode_pes(SrsFileWriter* writer, SrsTsMessage* msg, int16_t p
485 SrsAutoFree(SrsTsPacket, pkt); 485 SrsAutoFree(SrsTsPacket, pkt);
486 486
487 char* buf = new char[SRS_TS_PACKET_SIZE]; 487 char* buf = new char[SRS_TS_PACKET_SIZE];
488 - SrsAutoFree(char, buf); 488 + SrsAutoFreeA(char, buf);
489 489
490 // set the left bytes with 0xFF. 490 // set the left bytes with 0xFF.
491 int nb_buf = pkt->size(); 491 int nb_buf = pkt->size();
@@ -157,7 +157,7 @@ int SrsRawH264Stream::mux_sequence_header(string sps, string pps, u_int32_t dts, @@ -157,7 +157,7 @@ int SrsRawH264Stream::mux_sequence_header(string sps, string pps, u_int32_t dts,
157 + 3 + (int)sps.length() 157 + 3 + (int)sps.length()
158 + 3 + (int)pps.length(); 158 + 3 + (int)pps.length();
159 char* packet = new char[nb_packet]; 159 char* packet = new char[nb_packet];
160 - SrsAutoFree(char, packet); 160 + SrsAutoFreeA(char, packet);
161 161
162 // use stream to generate the h264 packet. 162 // use stream to generate the h264 packet.
163 SrsStream stream; 163 SrsStream stream;
@@ -236,7 +236,7 @@ int SrsRawH264Stream::mux_ipb_frame(char* frame, int nb_frame, string& ibp) @@ -236,7 +236,7 @@ int SrsRawH264Stream::mux_ipb_frame(char* frame, int nb_frame, string& ibp)
236 // NALUnit 236 // NALUnit
237 int nb_packet = 4 + nb_frame; 237 int nb_packet = 4 + nb_frame;
238 char* packet = new char[nb_packet]; 238 char* packet = new char[nb_packet];
239 - SrsAutoFree(char, packet); 239 + SrsAutoFreeA(char, packet);
240 240
241 // use stream to generate the h264 packet. 241 // use stream to generate the h264 packet.
242 SrsStream stream; 242 SrsStream stream;
@@ -487,7 +487,7 @@ namespace _srs_internal @@ -487,7 +487,7 @@ namespace _srs_internal
487 } 487 }
488 488
489 srs_assert(c1_digest != NULL); 489 srs_assert(c1_digest != NULL);
490 - SrsAutoFree(char, c1_digest); 490 + SrsAutoFreeA(char, c1_digest);
491 491
492 memcpy(digest.digest, c1_digest, 32); 492 memcpy(digest.digest, c1_digest, 32);
493 493
@@ -506,7 +506,7 @@ namespace _srs_internal @@ -506,7 +506,7 @@ namespace _srs_internal
506 } 506 }
507 507
508 srs_assert(c1_digest != NULL); 508 srs_assert(c1_digest != NULL);
509 - SrsAutoFree(char, c1_digest); 509 + SrsAutoFreeA(char, c1_digest);
510 510
511 is_valid = srs_bytes_equals(digest.digest, c1_digest, 32); 511 is_valid = srs_bytes_equals(digest.digest, c1_digest, 32);
512 512
@@ -546,7 +546,7 @@ namespace _srs_internal @@ -546,7 +546,7 @@ namespace _srs_internal
546 srs_verbose("calc s1 digest success."); 546 srs_verbose("calc s1 digest success.");
547 547
548 srs_assert(s1_digest != NULL); 548 srs_assert(s1_digest != NULL);
549 - SrsAutoFree(char, s1_digest); 549 + SrsAutoFreeA(char, s1_digest);
550 550
551 memcpy(digest.digest, s1_digest, 32); 551 memcpy(digest.digest, s1_digest, 32);
552 srs_verbose("copy s1 key success."); 552 srs_verbose("copy s1 key success.");
@@ -566,7 +566,7 @@ namespace _srs_internal @@ -566,7 +566,7 @@ namespace _srs_internal
566 } 566 }
567 567
568 srs_assert(s1_digest != NULL); 568 srs_assert(s1_digest != NULL);
569 - SrsAutoFree(char, s1_digest); 569 + SrsAutoFreeA(char, s1_digest);
570 570
571 is_valid = srs_bytes_equals(digest.digest, s1_digest, 32); 571 is_valid = srs_bytes_equals(digest.digest, s1_digest, 32);
572 572
@@ -585,7 +585,7 @@ namespace _srs_internal @@ -585,7 +585,7 @@ namespace _srs_internal
585 * @return a new allocated bytes, user must free it. 585 * @return a new allocated bytes, user must free it.
586 */ 586 */
587 char* c1s1_joined_bytes = new char[1536 -32]; 587 char* c1s1_joined_bytes = new char[1536 -32];
588 - SrsAutoFree(char, c1s1_joined_bytes); 588 + SrsAutoFreeA(char, c1s1_joined_bytes);
589 if ((ret = copy_to(owner, c1s1_joined_bytes, 1536 - 32, false)) != ERROR_SUCCESS) { 589 if ((ret = copy_to(owner, c1s1_joined_bytes, 1536 - 32, false)) != ERROR_SUCCESS) {
590 return ret; 590 return ret;
591 } 591 }
@@ -613,7 +613,7 @@ namespace _srs_internal @@ -613,7 +613,7 @@ namespace _srs_internal
613 * @return a new allocated bytes, user must free it. 613 * @return a new allocated bytes, user must free it.
614 */ 614 */
615 char* c1s1_joined_bytes = new char[1536 -32]; 615 char* c1s1_joined_bytes = new char[1536 -32];
616 - SrsAutoFree(char, c1s1_joined_bytes); 616 + SrsAutoFreeA(char, c1s1_joined_bytes);
617 if ((ret = copy_to(owner, c1s1_joined_bytes, 1536 - 32, false)) != ERROR_SUCCESS) { 617 if ((ret = copy_to(owner, c1s1_joined_bytes, 1536 - 32, false)) != ERROR_SUCCESS) {
618 return ret; 618 return ret;
619 } 619 }
@@ -548,7 +548,7 @@ int SrsRtspSdp::parse_fmtp_attribute(string attr) @@ -548,7 +548,7 @@ int SrsRtspSdp::parse_fmtp_attribute(string attr)
548 } 548 }
549 549
550 char* tmp_sh = new char[item_value.length()]; 550 char* tmp_sh = new char[item_value.length()];
551 - SrsAutoFree(char, tmp_sh); 551 + SrsAutoFreeA(char, tmp_sh);
552 int nb_tmp_sh = ff_hex_to_data((u_int8_t*)tmp_sh, item_value.c_str()); 552 int nb_tmp_sh = ff_hex_to_data((u_int8_t*)tmp_sh, item_value.c_str());
553 srs_assert(nb_tmp_sh > 0); 553 srs_assert(nb_tmp_sh > 0);
554 audio_sh.append(tmp_sh, nb_tmp_sh); 554 audio_sh.append(tmp_sh, nb_tmp_sh);
@@ -603,7 +603,7 @@ string SrsRtspSdp::base64_decode(string value) @@ -603,7 +603,7 @@ string SrsRtspSdp::base64_decode(string value)
603 603
604 int nb_output = (int)(value.length() * 2); 604 int nb_output = (int)(value.length() * 2);
605 u_int8_t* output = new u_int8_t[nb_output]; 605 u_int8_t* output = new u_int8_t[nb_output];
606 - SrsAutoFree(u_int8_t, output); 606 + SrsAutoFreeA(u_int8_t, output);
607 607
608 int ret = srs_av_base64_decode(output, (char*)value.c_str(), nb_output); 608 int ret = srs_av_base64_decode(output, (char*)value.c_str(), nb_output);
609 if (ret <= 0) { 609 if (ret <= 0) {