diff --git a/trunk/src/app/srs_app_caster_flv.cpp b/trunk/src/app/srs_app_caster_flv.cpp index 758bfaf7d7..ff992308b3 100644 --- a/trunk/src/app/srs_app_caster_flv.cpp +++ b/trunk/src/app/srs_app_caster_flv.cpp @@ -215,10 +215,7 @@ srs_error_t SrsDynamicHttpConn::proxy(ISrsHttpResponseWriter* w, ISrsHttpMessage output = o; srs_trace("flv: proxy %s:%d %s to %s", ip.c_str(), port, r->uri().c_str(), output.c_str()); - - char* buffer = new char[SRS_HTTP_FLV_STREAM_BUFFER]; - SrsAutoFreeA(char, buffer); - + ISrsHttpResponseReader* rr = r->body_reader(); SrsHttpFileReader reader(rr); SrsFlvDecoder dec; diff --git a/trunk/src/app/srs_app_conn.cpp b/trunk/src/app/srs_app_conn.cpp index 17033411ce..a70e21de46 100644 --- a/trunk/src/app/srs_app_conn.cpp +++ b/trunk/src/app/srs_app_conn.cpp @@ -902,19 +902,18 @@ srs_error_t SrsSslConnection::read(void* plaintext, size_t nn_plaintext, ssize_t if (r0 == -1 && r1 == SSL_ERROR_WANT_READ) { // TODO: Can we avoid copy? int nn_cipher = nn_plaintext; - char* cipher = new char[nn_cipher]; - SrsAutoFreeA(char, cipher); + SrsUniquePtr cipher(new char[nn_cipher]); // Read the cipher from SSL. ssize_t nn = 0; - if ((err = transport->read(cipher, nn_cipher, &nn)) != srs_success) { + if ((err = transport->read(cipher.get(), nn_cipher, &nn)) != srs_success) { return srs_error_wrap(err, "https: read"); } - int r0 = BIO_write(bio_in, cipher, nn); + int r0 = BIO_write(bio_in, cipher.get(), nn); if (r0 <= 0) { // TODO: 0 or -1 maybe block, use BIO_should_retry to check. - return srs_error_new(ERROR_HTTPS_READ, "BIO_write r0=%d, cipher=%p, size=%d", r0, cipher, nn); + return srs_error_new(ERROR_HTTPS_READ, "BIO_write r0=%d, cipher=%p, size=%d", r0, cipher.get(), nn); } continue; } diff --git a/trunk/src/app/srs_app_dvr.cpp b/trunk/src/app/srs_app_dvr.cpp index fc9b91c60d..f56c06da07 100644 --- a/trunk/src/app/srs_app_dvr.cpp +++ b/trunk/src/app/srs_app_dvr.cpp @@ -256,10 +256,8 @@ srs_error_t SrsDvrFlvSegmenter::refresh_metadata() int64_t cur = fs->tellg(); // buffer to write the size. - char* buf = new char[SrsAmf0Size::number()]; - SrsAutoFreeA(char, buf); - - SrsBuffer stream(buf, SrsAmf0Size::number()); + SrsUniquePtr buf(new char[SrsAmf0Size::number()]); + SrsBuffer stream(buf.get(), SrsAmf0Size::number()); // filesize to buf. SrsUniquePtr size(SrsAmf0Any::number((double)cur)); @@ -271,7 +269,7 @@ srs_error_t SrsDvrFlvSegmenter::refresh_metadata() // update the flesize. fs->seek2(filesize_offset); - if ((err = fs->write(buf, SrsAmf0Size::number(), NULL)) != srs_success) { + if ((err = fs->write(buf.get(), SrsAmf0Size::number(), NULL)) != srs_success) { return srs_error_wrap(err, "update filesize"); } @@ -285,7 +283,7 @@ srs_error_t SrsDvrFlvSegmenter::refresh_metadata() // update the duration fs->seek2(duration_offset); - if ((err = fs->write(buf, SrsAmf0Size::number(), NULL)) != srs_success) { + if ((err = fs->write(buf.get(), SrsAmf0Size::number(), NULL)) != srs_success) { return srs_error_wrap(err, "update duration"); } @@ -351,16 +349,15 @@ srs_error_t SrsDvrFlvSegmenter::encode_metadata(SrsSharedPtrMessage* metadata) obj->set("duration", SrsAmf0Any::number(0)); int size = name->total_size() + obj->total_size(); - char* payload = new char[size]; - SrsAutoFreeA(char, payload); - + SrsUniquePtr payload(new char[size]); + // 11B flv header, 3B object EOF, 8B number value, 1B number flag. duration_offset = fs->tellg() + size + 11 - SrsAmf0Size::object_eof() - SrsAmf0Size::number(); // 2B string flag, 8B number value, 8B string 'duration', 1B number flag filesize_offset = duration_offset - SrsAmf0Size::utf8("duration") - SrsAmf0Size::number(); // convert metadata to bytes. - SrsBuffer buf(payload, size); + SrsBuffer buf(payload.get(), size); if ((err = name->write(&buf)) != srs_success) { return srs_error_wrap(err, "write name"); @@ -370,7 +367,7 @@ srs_error_t SrsDvrFlvSegmenter::encode_metadata(SrsSharedPtrMessage* metadata) } // to flv file. - if ((err = enc->write_metadata(18, payload, size)) != srs_success) { + if ((err = enc->write_metadata(18, payload.get(), size)) != srs_success) { return srs_error_wrap(err, "write metadata"); } diff --git a/trunk/src/app/srs_app_hds.cpp b/trunk/src/app/srs_app_hds.cpp index 13bfc282a0..de9496eeb6 100644 --- a/trunk/src/app/srs_app_hds.cpp +++ b/trunk/src/app/srs_app_hds.cpp @@ -448,17 +448,16 @@ srs_error_t SrsHds::flush_bootstrap() srs_error_t err = srs_success; int size = 1024*100; - - char *start_abst = new char[1024*100]; - SrsAutoFreeA(char, start_abst); - + + SrsUniquePtr start_abst(new char[1024*100]); + int size_abst = 0; char *start_asrt = NULL; int size_asrt = 0; char *start_afrt = NULL; int size_afrt = 0; - SrsBuffer abst(start_abst, size); + SrsBuffer abst(start_abst.get(), size); // @see video_file_format_spec_v10_1 // page: 46 @@ -553,7 +552,7 @@ srs_error_t SrsHds::flush_bootstrap() abst.write_1bytes(1); size_abst += 1; - start_asrt = start_abst + size_abst; + start_asrt = start_abst.get() + size_abst; // follows by asrt abst.write_4bytes(0); @@ -621,7 +620,7 @@ srs_error_t SrsHds::flush_bootstrap() size_abst += 1; // follows by afrt - start_afrt = start_abst + size_abst; + start_afrt = start_abst.get() + size_abst; abst.write_4bytes(0); abst.write_string("afrt"); @@ -671,7 +670,7 @@ srs_error_t SrsHds::flush_bootstrap() update_box(start_afrt, size_afrt); size_abst += size_afrt; - update_box(start_abst, size_abst); + update_box(start_abst.get(), size_abst); string path = _srs_config->get_hds_path(hds_req->vhost) + "/" + hds_req->app + "/" + hds_req->stream +".abst"; @@ -680,7 +679,7 @@ srs_error_t SrsHds::flush_bootstrap() return srs_error_new(ERROR_HDS_OPEN_BOOTSTRAP_FAILED, "open bootstrap file failed, path=%s", path.c_str()); } - if (write(fd, start_abst, size_abst) != size_abst) { + if (write(fd, start_abst.get(), size_abst) != size_abst) { close(fd); return srs_error_new(ERROR_HDS_WRITE_BOOTSTRAP_FAILED, "write bootstrap file failed, path=", path.c_str()); } diff --git a/trunk/src/app/srs_app_http_hooks.cpp b/trunk/src/app/srs_app_http_hooks.cpp index 2948ccb433..31ea1e431e 100644 --- a/trunk/src/app/srs_app_http_hooks.cpp +++ b/trunk/src/app/srs_app_http_hooks.cpp @@ -434,14 +434,13 @@ srs_error_t SrsHttpHooks::on_hls_notify(SrsContextId c, std::string url, SrsRequ SrsUniquePtr msg(msg_raw); int nb_buf = srs_min(nb_notify, SRS_HTTP_READ_BUFFER); - char* buf = new char[nb_buf]; - SrsAutoFreeA(char, buf); - + SrsUniquePtr buf(new char[nb_buf]); + int nb_read = 0; ISrsHttpResponseReader* br = msg->body_reader(); while (nb_read < nb_notify && !br->eof()) { ssize_t nb_bytes = 0; - if ((err = br->read(buf, nb_buf, &nb_bytes)) != srs_success) { + if ((err = br->read(buf.get(), nb_buf, &nb_bytes)) != srs_success) { break; } nb_read += (int)nb_bytes; diff --git a/trunk/src/app/srs_app_http_static.cpp b/trunk/src/app/srs_app_http_static.cpp index b5858585a7..18ba11dfe0 100644 --- a/trunk/src/app/srs_app_http_static.cpp +++ b/trunk/src/app/srs_app_http_static.cpp @@ -427,9 +427,7 @@ srs_error_t SrsVodStream::serve_flv_stream(ISrsHttpResponseWriter* w, ISrsHttpMe } // save sequence header, send later - char* sh_data = NULL; int sh_size = 0; - if (true) { // send sequence header int64_t start = 0; @@ -440,9 +438,9 @@ srs_error_t SrsVodStream::serve_flv_stream(ISrsHttpResponseWriter* w, ISrsHttpMe return srs_error_new(ERROR_HTTP_REMUX_SEQUENCE_HEADER, "no sequence, size=%d", sh_size); } } - sh_data = new char[sh_size]; - SrsAutoFreeA(char, sh_data); - if ((err = fs->read(sh_data, sh_size, NULL)) != srs_success) { + + SrsUniquePtr sh_data(new char[sh_size]); + if ((err = fs->read(sh_data.get(), sh_size, NULL)) != srs_success) { return srs_error_wrap(err, "fs read"); } @@ -458,7 +456,7 @@ srs_error_t SrsVodStream::serve_flv_stream(ISrsHttpResponseWriter* w, ISrsHttpMe if ((err = w->write(flv_header, sizeof(flv_header))) != srs_success) { return srs_error_wrap(err, "write flv header"); } - if (sh_size > 0 && (err = w->write(sh_data, sh_size)) != srs_success) { + if (sh_size > 0 && (err = w->write(sh_data.get(), sh_size)) != srs_success) { return srs_error_wrap(err, "write sequence"); } diff --git a/trunk/src/app/srs_app_mpegts_udp.cpp b/trunk/src/app/srs_app_mpegts_udp.cpp index 3ddfda912d..84346c20e7 100644 --- a/trunk/src/app/srs_app_mpegts_udp.cpp +++ b/trunk/src/app/srs_app_mpegts_udp.cpp @@ -236,13 +236,12 @@ srs_error_t SrsMpegtsOverUdp::on_udp_bytes(string host, int port, char* buf, int } buffer->erase(buffer->length()); int nb_fbuf = fr.filesize(); - char* fbuf = new char[nb_fbuf]; - SrsAutoFreeA(char, fbuf); - if ((err = fr.read(fbuf, nb_fbuf, NULL)) != srs_success) { + SrsUniquePtr fbuf(new char[nb_fbuf]); + if ((err = fr.read(fbuf.get(), nb_fbuf, NULL)) != srs_success) { return srs_error_wrap(err, "read data"); } fr.close(); - buffer->append(fbuf, nb_fbuf); + buffer->append(fbuf.get(), nb_fbuf); #endif // find the sync byte of mpegts. diff --git a/trunk/src/app/srs_app_rtc_dtls.cpp b/trunk/src/app/srs_app_rtc_dtls.cpp index c83b5c29f0..df4aaa44ba 100644 --- a/trunk/src/app/srs_app_rtc_dtls.cpp +++ b/trunk/src/app/srs_app_rtc_dtls.cpp @@ -329,13 +329,12 @@ srs_error_t SrsDtlsCertificate::initialize() // TODO: FIXME: Unused variable. /*int r = */X509_digest(dtls_cert, EVP_sha256(), md, &n); - char* fp = new char[3 * n]; - SrsAutoFreeA(char, fp); - char *p = fp; + SrsUniquePtr fp(new char[3 * n]); + char* p = fp.get(); for (unsigned int i = 0; i < n; i++, ++p) { int nb = snprintf(p, 3, "%02X", md[i]); - srs_assert(nb > 0 && nb < (3 * n - (p - fp))); + srs_assert(nb > 0 && nb < (3 * n - (p - fp.get()))); p += nb; if(i < (n-1)) { @@ -345,7 +344,7 @@ srs_error_t SrsDtlsCertificate::initialize() } } - fingerprint.assign(fp, strlen(fp)); + fingerprint.assign(fp.get(), strlen(fp.get())); srs_trace("fingerprint=%s", fingerprint.c_str()); } @@ -985,10 +984,9 @@ srs_error_t SrsSRTP::initialize(string recv_key, std::string send_key) // init recv context policy.ssrc.type = ssrc_any_inbound; - uint8_t *rkey = new uint8_t[recv_key.size()]; - SrsAutoFreeA(uint8_t, rkey); - memcpy(rkey, recv_key.data(), recv_key.size()); - policy.key = rkey; + SrsUniquePtr rkey(new uint8_t[recv_key.size()]); + memcpy(rkey.get(), recv_key.data(), recv_key.size()); + policy.key = rkey.get(); srtp_err_status_t r0 = srtp_err_status_ok; if ((r0 = srtp_create(&recv_ctx_, &policy)) != srtp_err_status_ok) { @@ -996,10 +994,9 @@ srs_error_t SrsSRTP::initialize(string recv_key, std::string send_key) } policy.ssrc.type = ssrc_any_outbound; - uint8_t *skey = new uint8_t[send_key.size()]; - SrsAutoFreeA(uint8_t, skey); - memcpy(skey, send_key.data(), send_key.size()); - policy.key = skey; + SrsUniquePtr skey(new uint8_t[send_key.size()]); + memcpy(skey.get(), send_key.data(), send_key.size()); + policy.key = skey.get(); if ((r0 = srtp_create(&send_ctx_, &policy)) != srtp_err_status_ok) { return srs_error_new(ERROR_RTC_SRTP_INIT, "srtp create r0=%u", r0); diff --git a/trunk/src/app/srs_app_tencentcloud.cpp b/trunk/src/app/srs_app_tencentcloud.cpp index 91104875f9..e904f197f8 100644 --- a/trunk/src/app/srs_app_tencentcloud.cpp +++ b/trunk/src/app/srs_app_tencentcloud.cpp @@ -726,16 +726,15 @@ srs_error_t SrsClsClient::do_send_logs(ISrsEncoder* sugar, int count, int total) return srs_error_new(ERROR_CLS_EXCEED_SIZE, "exceed 5MB actual %d", size); } - char* buf = new char[size]; - SrsAutoFreeA(char, buf); + SrsUniquePtr buf(new char[size]); - memset(buf, 0, size); - SrsBuffer b(buf, size); + memset(buf.get(), 0, size); + SrsBuffer b(buf.get(), size); if ((err = sugar->encode(&b)) != srs_success) { return srs_error_wrap(err, "encode log"); } - string body(buf, size); + string body(buf.get(), size); // Write a CLS log to service specified by url. string url = "http://" + endpoint_ + ":80/structuredlog?topic_id=" + topic_; @@ -2166,16 +2165,15 @@ srs_error_t SrsApmClient::do_report() return srs_error_new(ERROR_APM_EXCEED_SIZE, "exceed 5MB actual %d", size); } - char* buf = new char[size]; - SrsAutoFreeA(char, buf); + SrsUniquePtr buf(new char[size]); - memset(buf, 0, size); - SrsBuffer b(buf, size); + memset(buf.get(), 0, size); + SrsBuffer b(buf.get(), size); if ((err = sugar->encode(&b)) != srs_success) { return srs_error_wrap(err, "encode log"); } - string body(buf, size); + string body(buf.get(), size); // Write a CLS log to service specified by url. string url = "http://" + endpoint_ + "/v1/traces"; diff --git a/trunk/src/core/srs_core_autofree.hpp b/trunk/src/core/srs_core_autofree.hpp index 463b4afb1f..1f020ee9d0 100644 --- a/trunk/src/core/srs_core_autofree.hpp +++ b/trunk/src/core/srs_core_autofree.hpp @@ -143,6 +143,55 @@ class SrsUniquePtr #endif }; +// The unique ptr for array objects, only support unique ptr, with limited APIs and features, +// see https://github.com/ossrs/srs/discussions/3667#discussioncomment-8969107 +// +// Usage: +// SrsUniquePtr ptr(new MyClass[10]); +// ptr[0]->do_something(); +template +class SrsUniquePtr +{ +private: + T* ptr_; +public: + SrsUniquePtr(T* ptr = NULL) { + ptr_ = ptr; + } + virtual ~SrsUniquePtr() { + delete[] ptr_; + } +public: + // Get the object. + T* get() { + return ptr_; + } + // Overload the [] operator. + T& operator[](std::size_t index) { + return ptr_[index]; + } + const T& operator[](std::size_t index) const { + return ptr_[index]; + } +private: + // Copy the unique ptr. + SrsUniquePtr(const SrsUniquePtr&) = delete; + // The assign operator. + SrsUniquePtr& operator=(const SrsUniquePtr&) = delete; +private: + // Overload the * operator. + T& operator*() = delete; + // Overload the bool operator. + operator bool() const = delete; +#if __cplusplus >= 201103L // C++11 +private: + // The move constructor. + SrsUniquePtr(SrsUniquePtr&&) = delete; + // The move assign operator. + SrsUniquePtr& operator=(SrsUniquePtr&&) = delete; +#endif +}; + // Shared ptr smart pointer, only support shared ptr, no weak ptr, no shared from this, no inheritance, // no comparing, see https://github.com/ossrs/srs/discussions/3667#discussioncomment-8969107 // diff --git a/trunk/src/kernel/srs_kernel_mp4.cpp b/trunk/src/kernel/srs_kernel_mp4.cpp index 8ca1de45f2..12ecb5ca2c 100644 --- a/trunk/src/kernel/srs_kernel_mp4.cpp +++ b/trunk/src/kernel/srs_kernel_mp4.cpp @@ -5788,16 +5788,15 @@ srs_error_t SrsMp4Encoder::initialize(ISrsWriteSeeker* ws) } int nb_data = mdat->sz_header(); - uint8_t* data = new uint8_t[nb_data]; - SrsAutoFreeA(uint8_t, data); + SrsUniquePtr data(new uint8_t[nb_data]); - SrsUniquePtr buffer(new SrsBuffer((char*)data, nb_data)); + SrsUniquePtr buffer(new SrsBuffer((char*)data.get(), nb_data)); if ((err = mdat->encode(buffer.get())) != srs_success) { return srs_error_wrap(err, "encode mdat"); } // TODO: FIXME: Ensure all bytes are writen. - if ((err = wsio->write(data, nb_data, NULL)) != srs_success) { + if ((err = wsio->write(data.get(), nb_data, NULL)) != srs_success) { return srs_error_wrap(err, "write mdat"); } @@ -6049,16 +6048,15 @@ srs_error_t SrsMp4Encoder::flush() } int nb_data = moov->nb_bytes(); - uint8_t* data = new uint8_t[nb_data]; - SrsAutoFreeA(uint8_t, data); + SrsUniquePtr data(new uint8_t[nb_data]); - SrsUniquePtr buffer(new SrsBuffer((char*)data, nb_data)); + SrsUniquePtr buffer(new SrsBuffer((char*)data.get(), nb_data)); if ((err = moov->encode(buffer.get())) != srs_success) { return srs_error_wrap(err, "encode moov"); } // TODO: FIXME: Ensure all bytes are writen. - if ((err = wsio->write(data, nb_data, NULL)) != srs_success) { + if ((err = wsio->write(data.get(), nb_data, NULL)) != srs_success) { return srs_error_wrap(err, "write moov"); } } @@ -6075,10 +6073,9 @@ srs_error_t SrsMp4Encoder::flush() mdat->update_size(); int nb_data = mdat->sz_header(); - uint8_t* data = new uint8_t[nb_data]; - SrsAutoFreeA(uint8_t, data); + SrsUniquePtr data(new uint8_t[nb_data]); - SrsUniquePtr buffer(new SrsBuffer((char*)data, nb_data)); + SrsUniquePtr buffer(new SrsBuffer((char*)data.get(), nb_data)); if ((err = mdat->encode(buffer.get())) != srs_success) { return srs_error_wrap(err, "encode mdat"); } @@ -6099,7 +6096,7 @@ srs_error_t SrsMp4Encoder::flush() } // TODO: FIXME: Ensure all bytes are writen. - if ((err = wsio->write(data, nb_data, NULL)) != srs_success) { + if ((err = wsio->write(data.get(), nb_data, NULL)) != srs_success) { return srs_error_wrap(err, "write mdat"); } } @@ -6611,16 +6608,15 @@ srs_error_t SrsMp4M2tsSegmentEncoder::flush(uint64_t& dts) // Write mdat. if (true) { int nb_data = mdat->sz_header(); - uint8_t* data = new uint8_t[nb_data]; - SrsAutoFreeA(uint8_t, data); + SrsUniquePtr data(new uint8_t[nb_data]); - SrsUniquePtr buffer(new SrsBuffer((char*)data, nb_data)); + SrsUniquePtr buffer(new SrsBuffer((char*)data.get(), nb_data)); if ((err = mdat->encode(buffer.get())) != srs_success) { return srs_error_wrap(err, "encode mdat"); } // TODO: FIXME: Ensure all bytes are writen. - if ((err = writer->write(data, nb_data, NULL)) != srs_success) { + if ((err = writer->write(data.get(), nb_data, NULL)) != srs_success) { return srs_error_wrap(err, "write mdat"); } diff --git a/trunk/src/kernel/srs_kernel_ts.cpp b/trunk/src/kernel/srs_kernel_ts.cpp index d510cf1cd8..5ae6e3e7d5 100644 --- a/trunk/src/kernel/srs_kernel_ts.cpp +++ b/trunk/src/kernel/srs_kernel_ts.cpp @@ -384,20 +384,19 @@ srs_error_t SrsTsContext::encode_pat_pmt(ISrsStreamWriter* writer, int16_t vpid, SrsUniquePtr pkt(SrsTsPacket::create_pat(this, pmt_number, pmt_pid)); pkt->sync_byte = sync_byte; - - char* buf = new char[SRS_TS_PACKET_SIZE]; - SrsAutoFreeA(char, buf); - + + SrsUniquePtr buf(new char[SRS_TS_PACKET_SIZE]); + // set the left bytes with 0xFF. int nb_buf = pkt->size(); srs_assert(nb_buf < SRS_TS_PACKET_SIZE); - memset(buf + nb_buf, 0xFF, SRS_TS_PACKET_SIZE - nb_buf); + memset(buf.get() + nb_buf, 0xFF, SRS_TS_PACKET_SIZE - nb_buf); - SrsBuffer stream(buf, nb_buf); + SrsBuffer stream(buf.get(), nb_buf); if ((err = pkt->encode(&stream)) != srs_success) { return srs_error_wrap(err, "ts: encode packet"); } - if ((err = writer->write(buf, SRS_TS_PACKET_SIZE, NULL)) != srs_success) { + if ((err = writer->write(buf.get(), SRS_TS_PACKET_SIZE, NULL)) != srs_success) { return srs_error_wrap(err, "ts: write packet"); } } @@ -405,20 +404,19 @@ srs_error_t SrsTsContext::encode_pat_pmt(ISrsStreamWriter* writer, int16_t vpid, SrsUniquePtr pkt(SrsTsPacket::create_pmt(this, pmt_number, pmt_pid, vpid, vs, apid, as)); pkt->sync_byte = sync_byte; - - char* buf = new char[SRS_TS_PACKET_SIZE]; - SrsAutoFreeA(char, buf); - + + SrsUniquePtr buf(new char[SRS_TS_PACKET_SIZE]); + // set the left bytes with 0xFF. int nb_buf = pkt->size(); srs_assert(nb_buf < SRS_TS_PACKET_SIZE); - memset(buf + nb_buf, 0xFF, SRS_TS_PACKET_SIZE - nb_buf); + memset(buf.get() + nb_buf, 0xFF, SRS_TS_PACKET_SIZE - nb_buf); - SrsBuffer stream(buf, nb_buf); + SrsBuffer stream(buf.get(), nb_buf); if ((err = pkt->encode(&stream)) != srs_success) { return srs_error_wrap(err, "ts: encode packet"); } - if ((err = writer->write(buf, SRS_TS_PACKET_SIZE, NULL)) != srs_success) { + if ((err = writer->write(buf.get(), SRS_TS_PACKET_SIZE, NULL)) != srs_success) { return srs_error_wrap(err, "ts: write packet"); } } @@ -493,10 +491,9 @@ srs_error_t SrsTsContext::encode_pes(ISrsStreamWriter* writer, SrsTsMessage* msg SrsUniquePtr pkt(pkt_raw); pkt->sync_byte = sync_byte; - - char* buf = new char[SRS_TS_PACKET_SIZE]; - SrsAutoFreeA(char, buf); - + + SrsUniquePtr buf(new char[SRS_TS_PACKET_SIZE]); + // set the left bytes with 0xFF. int nb_buf = pkt->size(); srs_assert(nb_buf < SRS_TS_PACKET_SIZE); @@ -505,7 +502,7 @@ srs_error_t SrsTsContext::encode_pes(ISrsStreamWriter* writer, SrsTsMessage* msg int nb_stuffings = SRS_TS_PACKET_SIZE - nb_buf - left; if (nb_stuffings > 0) { // set all bytes to stuffings. - memset(buf, 0xFF, SRS_TS_PACKET_SIZE); + memset(buf.get(), 0xFF, SRS_TS_PACKET_SIZE); // padding with stuffings. pkt->padding(nb_stuffings); @@ -518,14 +515,14 @@ srs_error_t SrsTsContext::encode_pes(ISrsStreamWriter* writer, SrsTsMessage* msg nb_stuffings = SRS_TS_PACKET_SIZE - nb_buf - left; srs_assert(nb_stuffings == 0); } - memcpy(buf + nb_buf, p, left); + memcpy(buf.get() + nb_buf, p, left); p += left; - SrsBuffer stream(buf, nb_buf); + SrsBuffer stream(buf.get(), nb_buf); if ((err = pkt->encode(&stream)) != srs_success) { return srs_error_wrap(err, "ts: encode packet"); } - if ((err = writer->write(buf, SRS_TS_PACKET_SIZE, NULL)) != srs_success) { + if ((err = writer->write(buf.get(), SRS_TS_PACKET_SIZE, NULL)) != srs_success) { return srs_error_wrap(err, "ts: write packet"); } } @@ -2771,14 +2768,13 @@ srs_error_t SrsEncFileWriter::write(void* data, size_t count, ssize_t* pnwrite) if (nb_buf == HLS_AES_ENCRYPT_BLOCK_LENGTH) { nb_buf = 0; - - char* cipher = new char[HLS_AES_ENCRYPT_BLOCK_LENGTH]; - SrsAutoFreeA(char, cipher); - + + SrsUniquePtr cipher(new char[HLS_AES_ENCRYPT_BLOCK_LENGTH]); + AES_KEY* k = (AES_KEY*)key; - AES_cbc_encrypt((unsigned char *)buf, (unsigned char *)cipher, HLS_AES_ENCRYPT_BLOCK_LENGTH, k, iv, AES_ENCRYPT); + AES_cbc_encrypt((unsigned char *)buf, (unsigned char *)cipher.get(), HLS_AES_ENCRYPT_BLOCK_LENGTH, k, iv, AES_ENCRYPT); - if ((err = SrsFileWriter::write(cipher, HLS_AES_ENCRYPT_BLOCK_LENGTH, pnwrite)) != srs_success) { + if ((err = SrsFileWriter::write(cipher.get(), HLS_AES_ENCRYPT_BLOCK_LENGTH, pnwrite)) != srs_success) { return srs_error_wrap(err, "write cipher"); } } @@ -2807,15 +2803,14 @@ void SrsEncFileWriter::close() if (nb_padding > 0) { memset(buf + nb_buf, nb_padding, nb_padding); } - - char* cipher = new char[nb_buf + nb_padding]; - SrsAutoFreeA(char, cipher); - + + SrsUniquePtr cipher(new char[nb_buf + nb_padding]); + AES_KEY* k = (AES_KEY*)key; - AES_cbc_encrypt((unsigned char *)buf, (unsigned char *)cipher, nb_buf + nb_padding, k, iv, AES_ENCRYPT); + AES_cbc_encrypt((unsigned char *)buf, (unsigned char *)cipher.get(), nb_buf + nb_padding, k, iv, AES_ENCRYPT); srs_error_t err = srs_success; - if ((err = SrsFileWriter::write(cipher, nb_buf + nb_padding, NULL)) != srs_success) { + if ((err = SrsFileWriter::write(cipher.get(), nb_buf + nb_padding, NULL)) != srs_success) { srs_warn("ignore err %s", srs_error_desc(err).c_str()); srs_error_reset(err); } diff --git a/trunk/src/protocol/srs_protocol_http_client.cpp b/trunk/src/protocol/srs_protocol_http_client.cpp index eff241e9d9..633288c4b9 100644 --- a/trunk/src/protocol/srs_protocol_http_client.cpp +++ b/trunk/src/protocol/srs_protocol_http_client.cpp @@ -201,19 +201,18 @@ srs_error_t SrsSslClient::read(void* plaintext, size_t nn_plaintext, ssize_t* nr if (r0 == -1 && r1 == SSL_ERROR_WANT_READ) { // TODO: Can we avoid copy? int nn_cipher = nn_plaintext; - char* cipher = new char[nn_cipher]; - SrsAutoFreeA(char, cipher); + SrsUniquePtr cipher(new char[nn_cipher]); // Read the cipher from SSL. ssize_t nn = 0; - if ((err = transport->read(cipher, nn_cipher, &nn)) != srs_success) { + if ((err = transport->read(cipher.get(), nn_cipher, &nn)) != srs_success) { return srs_error_wrap(err, "https: read"); } - int r0 = BIO_write(bio_in, cipher, nn); + int r0 = BIO_write(bio_in, cipher.get(), nn); if (r0 <= 0) { // TODO: 0 or -1 maybe block, use BIO_should_retry to check. - return srs_error_new(ERROR_HTTPS_READ, "BIO_write r0=%d, cipher=%p, size=%d", r0, cipher, nn); + return srs_error_new(ERROR_HTTPS_READ, "BIO_write r0=%d, cipher=%p, size=%d", r0, cipher.get(), nn); } continue; } diff --git a/trunk/src/protocol/srs_protocol_http_stack.cpp b/trunk/src/protocol/srs_protocol_http_stack.cpp index 60ed829b5d..b280fca387 100644 --- a/trunk/src/protocol/srs_protocol_http_stack.cpp +++ b/trunk/src/protocol/srs_protocol_http_stack.cpp @@ -594,18 +594,17 @@ srs_error_t SrsHttpFileServer::copy(ISrsHttpResponseWriter* w, SrsFileReader* fs srs_error_t err = srs_success; int64_t left = size; - char* buf = new char[SRS_HTTP_TS_SEND_BUFFER_SIZE]; - SrsAutoFreeA(char, buf); - + SrsUniquePtr buf(new char[SRS_HTTP_TS_SEND_BUFFER_SIZE]); + while (left > 0) { ssize_t nread = -1; int max_read = srs_min(left, SRS_HTTP_TS_SEND_BUFFER_SIZE); - if ((err = fs->read(buf, max_read, &nread)) != srs_success) { + if ((err = fs->read(buf.get(), max_read, &nread)) != srs_success) { return srs_error_wrap(err, "read limit=%d, left=%" PRId64, max_read, left); } left -= nread; - if ((err = w->write(buf, (int)nread)) != srs_success) { + if ((err = w->write(buf.get(), (int)nread)) != srs_success) { return srs_error_wrap(err, "write limit=%d, bytes=%d, left=%" PRId64, max_read, (int)nread, left); } } diff --git a/trunk/src/protocol/srs_protocol_raw_avc.cpp b/trunk/src/protocol/srs_protocol_raw_avc.cpp index 2e1ea9f77b..e25a87f753 100644 --- a/trunk/src/protocol/srs_protocol_raw_avc.cpp +++ b/trunk/src/protocol/srs_protocol_raw_avc.cpp @@ -125,11 +125,10 @@ srs_error_t SrsRawH264Stream::mux_sequence_header(string sps, string pps, string // Nbytes of pps: // pictureParameterSetNALUnit int nb_packet = 5 + (3 + (int)sps.length()) + (3 + (int)pps.length()); - char* packet = new char[nb_packet]; - SrsAutoFreeA(char, packet); - + SrsUniquePtr packet(new char[nb_packet]); + // use stream to generate the h264 packet. - SrsBuffer stream(packet, nb_packet); + SrsBuffer stream(packet.get(), nb_packet); // decode the SPS: // @see: 7.3.2.1.1, ISO_IEC_14496-10-AVC-2012.pdf, page 62 @@ -186,7 +185,7 @@ srs_error_t SrsRawH264Stream::mux_sequence_header(string sps, string pps, string // 5.3.4.2.1 Syntax, ISO_IEC_14496-15-AVC-format-2012.pdf, page 16 // profile_idc == 100 || profile_idc == 110 || profile_idc == 122 || profile_idc == 144 - sh = string(packet, nb_packet); + sh = string(packet.get(), nb_packet); return err; } @@ -200,11 +199,10 @@ srs_error_t SrsRawH264Stream::mux_ipb_frame(char* frame, int nb_frame, string& i // Nbytes of nalu. // NALUnit int nb_packet = 4 + nb_frame; - char* packet = new char[nb_packet]; - SrsAutoFreeA(char, packet); - + SrsUniquePtr packet(new char[nb_packet]); + // use stream to generate the h264 packet. - SrsBuffer stream(packet, nb_packet); + SrsBuffer stream(packet.get(), nb_packet); // 5.3.4.2.1 Syntax, ISO_IEC_14496-15-AVC-format-2012.pdf, page 16 // lengthSizeMinusOne, or NAL_unit_length, always use 4bytes size @@ -217,7 +215,7 @@ srs_error_t SrsRawH264Stream::mux_ipb_frame(char* frame, int nb_frame, string& i // NALUnit stream.write_bytes(frame, nb_frame); - ibp = string(packet, nb_packet); + ibp = string(packet.get(), nb_packet); return err; } @@ -406,11 +404,10 @@ srs_error_t SrsRawHEVCStream::mux_sequence_header(std::string vps, std::string s } int nb_packet = 23 + 5 + (int)vps.length() + 5 + (int)sps.length() + 5 + pps_size - 2; - char* packet = new char[nb_packet]; - SrsAutoFreeA(char, packet); + SrsUniquePtr packet(new char[nb_packet]); // use stream to generate the hevc packet. - SrsBuffer stream(packet, nb_packet); + SrsBuffer stream(packet.get(), nb_packet); SrsFormat format; if ((err = format.initialize()) != srs_success) { @@ -511,7 +508,7 @@ srs_error_t SrsRawHEVCStream::mux_sequence_header(std::string vps, std::string s } } - hvcC = string(packet, nb_packet); + hvcC = string(packet.get(), nb_packet); return err; } @@ -525,11 +522,10 @@ srs_error_t SrsRawHEVCStream::mux_ipb_frame(char *frame, int nb_frame, std::stri // Nbytes of nalu. // NALUnit int nb_packet = 4 + nb_frame; - char *packet = new char[nb_packet]; - SrsAutoFreeA(char, packet); + SrsUniquePtr packet(new char[nb_packet]); // use stream to generate the h265 packet. - SrsBuffer stream(packet, nb_packet); + SrsBuffer stream(packet.get(), nb_packet); // 5.3.4.2.1 Syntax, ISO_IEC_14496-15-AVC-format-2012.pdf, page 16 // lengthSizeMinusOne, or NAL_unit_length, always use 4bytes size @@ -542,7 +538,7 @@ srs_error_t SrsRawHEVCStream::mux_ipb_frame(char *frame, int nb_frame, std::stri // NALUnit stream.write_bytes(frame, nb_frame); - ibp = string(packet, nb_packet); + ibp = string(packet.get(), nb_packet); return err; } diff --git a/trunk/src/protocol/srs_protocol_rtmp_handshake.cpp b/trunk/src/protocol/srs_protocol_rtmp_handshake.cpp index 0e8c9f097e..868b50f6ce 100644 --- a/trunk/src/protocol/srs_protocol_rtmp_handshake.cpp +++ b/trunk/src/protocol/srs_protocol_rtmp_handshake.cpp @@ -522,16 +522,16 @@ namespace srs_internal srs_error_t err = srs_success; // generate digest - char* c1_digest = NULL; + char* c1_digest_raw = NULL; - if ((err = calc_c1_digest(owner, c1_digest)) != srs_success) { + if ((err = calc_c1_digest(owner, c1_digest_raw)) != srs_success) { return srs_error_wrap(err, "sign c1"); } - srs_assert(c1_digest != NULL); - SrsAutoFreeA(char, c1_digest); - - memcpy(digest.digest, c1_digest, 32); + srs_assert(c1_digest_raw != NULL); + SrsUniquePtr c1_digest(c1_digest_raw); + + memcpy(digest.digest, c1_digest.get(), 32); return err; } @@ -540,16 +540,16 @@ namespace srs_internal { srs_error_t err = srs_success; - char* c1_digest = NULL; + char* c1_digest_raw = NULL; - if ((err = calc_c1_digest(owner, c1_digest)) != srs_success) { + if ((err = calc_c1_digest(owner, c1_digest_raw)) != srs_success) { return srs_error_wrap(err, "validate c1"); } - srs_assert(c1_digest != NULL); - SrsAutoFreeA(char, c1_digest); + srs_assert(c1_digest_raw != NULL); + SrsUniquePtr c1_digest(c1_digest_raw); - is_valid = srs_bytes_equals(digest.digest, c1_digest, 32); + is_valid = srs_bytes_equals(digest.digest, c1_digest.get(), 32); return err; } @@ -576,15 +576,15 @@ namespace srs_internal // TODO: FIXME: use the actual key size. //srs_assert(pkey_size == 128); - char* s1_digest = NULL; - if ((err = calc_s1_digest(owner, s1_digest)) != srs_success) { + char* s1_digest_raw = NULL; + if ((err = calc_s1_digest(owner, s1_digest_raw)) != srs_success) { return srs_error_wrap(err, "calc s1 digest"); } - srs_assert(s1_digest != NULL); - SrsAutoFreeA(char, s1_digest); - - memcpy(digest.digest, s1_digest, 32); + srs_assert(s1_digest_raw != NULL); + SrsUniquePtr s1_digest(s1_digest_raw); + + memcpy(digest.digest, s1_digest.get(), 32); return err; } @@ -593,16 +593,16 @@ namespace srs_internal { srs_error_t err = srs_success; - char* s1_digest = NULL; + char* s1_digest_raw = NULL; - if ((err = calc_s1_digest(owner, s1_digest)) != srs_success) { + if ((err = calc_s1_digest(owner, s1_digest_raw)) != srs_success) { return srs_error_wrap(err, "validate s1"); } - srs_assert(s1_digest != NULL); - SrsAutoFreeA(char, s1_digest); + srs_assert(s1_digest_raw != NULL); + SrsUniquePtr s1_digest(s1_digest_raw); - is_valid = srs_bytes_equals(digest.digest, s1_digest, 32); + is_valid = srs_bytes_equals(digest.digest, s1_digest.get(), 32); return err; } @@ -611,21 +611,18 @@ namespace srs_internal { srs_error_t err = srs_success; - /** - * c1s1 is splited by digest: - * c1s1-part1: n bytes (time, version, key and digest-part1). - * digest-data: 32bytes - * c1s1-part2: (1536-n-32)bytes (digest-part2) - * @return a new allocated bytes, user must free it. - */ - char* c1s1_joined_bytes = new char[1536 -32]; - SrsAutoFreeA(char, c1s1_joined_bytes); - if ((err = copy_to(owner, c1s1_joined_bytes, 1536 - 32, false)) != srs_success) { + // c1s1 is splited by digest: + // c1s1-part1: n bytes (time, version, key and digest-part1). + // digest-data: 32bytes + // c1s1-part2: (1536-n-32)bytes (digest-part2) + // @return a new allocated bytes, user must free it. + SrsUniquePtr c1s1_joined_bytes(new char[1536 -32]); + if ((err = copy_to(owner, c1s1_joined_bytes.get(), 1536 - 32, false)) != srs_success) { return srs_error_wrap(err, "copy bytes"); } c1_digest = new char[SRS_OpensslHashSize]; - if ((err = openssl_HMACsha256(SrsGenuineFPKey, 30, c1s1_joined_bytes, 1536 - 32, c1_digest)) != srs_success) { + if ((err = openssl_HMACsha256(SrsGenuineFPKey, 30, c1s1_joined_bytes.get(), 1536 - 32, c1_digest)) != srs_success) { srs_freepa(c1_digest); return srs_error_wrap(err, "calc c1 digest"); } @@ -637,21 +634,18 @@ namespace srs_internal { srs_error_t err = srs_success; - /** - * c1s1 is splited by digest: - * c1s1-part1: n bytes (time, version, key and digest-part1). - * digest-data: 32bytes - * c1s1-part2: (1536-n-32)bytes (digest-part2) - * @return a new allocated bytes, user must free it. - */ - char* c1s1_joined_bytes = new char[1536 -32]; - SrsAutoFreeA(char, c1s1_joined_bytes); - if ((err = copy_to(owner, c1s1_joined_bytes, 1536 - 32, false)) != srs_success) { + // c1s1 is splited by digest: + // c1s1-part1: n bytes (time, version, key and digest-part1). + // digest-data: 32bytes + // c1s1-part2: (1536-n-32)bytes (digest-part2) + // @return a new allocated bytes, user must free it. + SrsUniquePtr c1s1_joined_bytes(new char[1536 -32]); + if ((err = copy_to(owner, c1s1_joined_bytes.get(), 1536 - 32, false)) != srs_success) { return srs_error_wrap(err, "copy bytes"); } s1_digest = new char[SRS_OpensslHashSize]; - if ((err = openssl_HMACsha256(SrsGenuineFMSKey, 36, c1s1_joined_bytes, 1536 - 32, s1_digest)) != srs_success) { + if ((err = openssl_HMACsha256(SrsGenuineFMSKey, 36, c1s1_joined_bytes.get(), 1536 - 32, s1_digest)) != srs_success) { srs_freepa(s1_digest); return srs_error_wrap(err, "calc s1 digest"); } diff --git a/trunk/src/protocol/srs_protocol_utility.cpp b/trunk/src/protocol/srs_protocol_utility.cpp index 237538d134..749ea3342f 100644 --- a/trunk/src/protocol/srs_protocol_utility.cpp +++ b/trunk/src/protocol/srs_protocol_utility.cpp @@ -920,13 +920,12 @@ srs_error_t srs_ioutil_read_all(ISrsReader* in, std::string& content) srs_error_t err = srs_success; // Cache to read, it might cause coroutine switch, so we use local cache here. - char* buf = new char[SRS_HTTP_READ_CACHE_BYTES]; - SrsAutoFreeA(char, buf); + SrsUniquePtr buf(new char[SRS_HTTP_READ_CACHE_BYTES]); // Whatever, read util EOF. while (true) { ssize_t nb_read = 0; - if ((err = in->read(buf, SRS_HTTP_READ_CACHE_BYTES, &nb_read)) != srs_success) { + if ((err = in->read(buf.get(), SRS_HTTP_READ_CACHE_BYTES, &nb_read)) != srs_success) { int code = srs_error_code(err); if (code == ERROR_SYSTEM_FILE_EOF || code == ERROR_HTTP_RESPONSE_EOF || code == ERROR_HTTP_REQUEST_EOF || code == ERROR_HTTP_STREAM_EOF @@ -938,7 +937,7 @@ srs_error_t srs_ioutil_read_all(ISrsReader* in, std::string& content) } if (nb_read > 0) { - content.append(buf, nb_read); + content.append(buf.get(), nb_read); } } diff --git a/trunk/src/utest/srs_utest_amf0.cpp b/trunk/src/utest/srs_utest_amf0.cpp index 33fc762fbc..fdf3ccd342 100644 --- a/trunk/src/utest/srs_utest_amf0.cpp +++ b/trunk/src/utest/srs_utest_amf0.cpp @@ -77,7 +77,7 @@ VOID TEST(ProtocolAMF0Test, ScenarioMain) EXPECT_EQ(0x03, bytes[0]); EXPECT_EQ(0x09, bytes[nb_bytes - 1]); } - SrsAutoFreeA(char, bytes); + SrsUniquePtr bytes_uptr(bytes); // decoding amf0 object from bytes // when user know the schema @@ -1289,7 +1289,7 @@ VOID TEST(ProtocolAMF0Test, InterfacesString) // For marshal and unmarshal. char* bb = new char[p->total_size()]; - SrsAutoFreeA(char, bb); + SrsUniquePtr bb_uptr(bb); SrsBuffer b(bb, p->total_size()); HELPER_EXPECT_SUCCESS(p->write(&b)); @@ -1419,7 +1419,7 @@ VOID TEST(ProtocolAMF0Test, InterfacesBoolean) // For marshal and unmarshal. char* bb = new char[p->total_size()]; - SrsAutoFreeA(char, bb); + SrsUniquePtr bb_uptr(bb); SrsBuffer b(bb, p->total_size()); HELPER_EXPECT_SUCCESS(p->write(&b)); @@ -1517,7 +1517,7 @@ VOID TEST(ProtocolAMF0Test, InterfacesNumber) // For marshal and unmarshal. char* bb = new char[p->total_size()]; - SrsAutoFreeA(char, bb); + SrsUniquePtr bb_uptr(bb); SrsBuffer b(bb, p->total_size()); HELPER_EXPECT_SUCCESS(p->write(&b)); @@ -1603,7 +1603,7 @@ VOID TEST(ProtocolAMF0Test, InterfacesDate) // For marshal and unmarshal. char* bb = new char[p->total_size()]; - SrsAutoFreeA(char, bb); + SrsUniquePtr bb_uptr(bb); SrsBuffer b(bb, p->total_size()); HELPER_EXPECT_SUCCESS(p->write(&b)); @@ -1647,7 +1647,7 @@ VOID TEST(ProtocolAMF0Test, InterfacesNull) // For marshal and unmarshal. char* bb = new char[p->total_size()]; - SrsAutoFreeA(char, bb); + SrsUniquePtr bb_uptr(bb); SrsBuffer b(bb, p->total_size()); HELPER_EXPECT_SUCCESS(p->write(&b)); @@ -1713,7 +1713,7 @@ VOID TEST(ProtocolAMF0Test, InterfacesUndefined) // For marshal and unmarshal. char* bb = new char[p->total_size()]; - SrsAutoFreeA(char, bb); + SrsUniquePtr bb_uptr(bb); SrsBuffer b(bb, p->total_size()); HELPER_EXPECT_SUCCESS(p->write(&b)); @@ -1780,7 +1780,7 @@ VOID TEST(ProtocolAMF0Test, InterfacesObject) // For marshal and unmarshal. char* bb = new char[p->total_size()]; - SrsAutoFreeA(char, bb); + SrsUniquePtr bb_uptr(bb); SrsBuffer b(bb, p->total_size()); HELPER_EXPECT_SUCCESS(p->write(&b)); @@ -1985,7 +1985,7 @@ VOID TEST(ProtocolAMF0Test, InterfacesObjectEOF) // For marshal and unmarshal. char* bb = new char[p->total_size()]; - SrsAutoFreeA(char, bb); + SrsUniquePtr bb_uptr(bb); SrsBuffer b(bb, p->total_size()); HELPER_EXPECT_SUCCESS(p->write(&b)); @@ -2077,7 +2077,7 @@ VOID TEST(ProtocolAMF0Test, InterfacesEcmaArray) // For marshal and unmarshal. char* bb = new char[p->total_size()]; - SrsAutoFreeA(char, bb); + SrsUniquePtr bb_uptr(bb); SrsBuffer b(bb, p->total_size()); HELPER_EXPECT_SUCCESS(p->write(&b)); @@ -2205,7 +2205,7 @@ VOID TEST(ProtocolAMF0Test, InterfacesStrictArray) // For marshal and unmarshal. char* bb = new char[p->total_size()]; - SrsAutoFreeA(char, bb); + SrsUniquePtr bb_uptr(bb); SrsBuffer b(bb, p->total_size()); HELPER_EXPECT_SUCCESS(p->write(&b)); diff --git a/trunk/src/utest/srs_utest_core.cpp b/trunk/src/utest/srs_utest_core.cpp index 58115fcd99..85c0946559 100644 --- a/trunk/src/utest/srs_utest_core.cpp +++ b/trunk/src/utest/srs_utest_core.cpp @@ -176,10 +176,10 @@ class MockWrapper public: MockWrapper(int* p) { ptr = p; - *ptr = *ptr + 1; + if (ptr) *ptr = *ptr + 1; } ~MockWrapper() { - *ptr = *ptr - 1; + if (ptr) *ptr = *ptr - 1; } }; @@ -462,3 +462,29 @@ VOID TEST(CoreSmartPtr, UniquePtrNormal) EXPECT_EQ(100, *ptr); } +VOID TEST(CoreSmartPtr, UniquePtrArray) +{ + if (true) { + int* ptr = new int[100]; + ptr[0] = 100; + + SrsUniquePtr p(ptr); + EXPECT_EQ(100, *p.get()); + } + + int* ptr = new int(100); + SrsUniquePtr ptr_uptr(ptr); + EXPECT_EQ(100, *ptr); + + if (true) { + SrsUniquePtr p(new MockWrapper[1]{MockWrapper(ptr)}); + EXPECT_EQ(101, *ptr); + EXPECT_EQ(101, *(p[0].ptr)); + + SrsUniquePtr p0(new MockWrapper[1]{MockWrapper(ptr)}); + EXPECT_EQ(102, *ptr); + EXPECT_EQ(102, *(p0[0].ptr)); + } + EXPECT_EQ(100, *ptr); +} + diff --git a/trunk/src/utest/srs_utest_kernel.cpp b/trunk/src/utest/srs_utest_kernel.cpp index d781ca9661..00875638d1 100644 --- a/trunk/src/utest/srs_utest_kernel.cpp +++ b/trunk/src/utest/srs_utest_kernel.cpp @@ -6293,7 +6293,7 @@ VOID TEST(KernelMP4Test, CoverMP4M2tsSegmentEncoder) HELPER_EXPECT_SUCCESS(fmt.on_video(0, (char*)raw, sizeof(raw))); uint8_t* cp = mock_copy_bytes(fmt.raw, fmt.nb_raw); - SrsAutoFreeA(uint8_t, cp); + SrsUniquePtr cp_uptr(cp); HELPER_EXPECT_SUCCESS(enc.write_sample( SrsMp4HandlerTypeVIDE, fmt.video->frame_type, 0, 0, cp, fmt.nb_raw )); @@ -6306,7 +6306,7 @@ VOID TEST(KernelMP4Test, CoverMP4M2tsSegmentEncoder) HELPER_EXPECT_SUCCESS(fmt.on_audio(0, (char*)raw, sizeof(raw))); uint8_t* cp = mock_copy_bytes(fmt.raw, fmt.nb_raw); - SrsAutoFreeA(uint8_t, cp); + SrsUniquePtr cp_uptr(cp); HELPER_EXPECT_SUCCESS(enc.write_sample( SrsMp4HandlerTypeSOUN, 0x00, 0, 0, cp, fmt.nb_raw )); @@ -6325,7 +6325,7 @@ VOID TEST(KernelMP4Test, CoverMP4M2tsSegmentEncoder) HELPER_EXPECT_SUCCESS(fmt.on_audio(0, (char*)raw, sizeof(raw))); uint8_t* cp = mock_copy_bytes(fmt.raw, fmt.nb_raw); - SrsAutoFreeA(uint8_t, cp); + SrsUniquePtr cp_uptr(cp); HELPER_EXPECT_SUCCESS(enc.write_sample( SrsMp4HandlerTypeSOUN, 0x00, 34, 34, cp, fmt.nb_raw )); @@ -6347,7 +6347,7 @@ VOID TEST(KernelMP4Test, CoverMP4M2tsSegmentEncoder) HELPER_EXPECT_SUCCESS(fmt.on_video(0, (char*)raw, sizeof(raw))); uint8_t* cp = mock_copy_bytes(fmt.raw, fmt.nb_raw); - SrsAutoFreeA(uint8_t, cp); + SrsUniquePtr cp_uptr(cp); HELPER_EXPECT_SUCCESS(enc.write_sample( SrsMp4HandlerTypeVIDE, fmt.video->frame_type, 40, 40, cp, fmt.nb_raw )); diff --git a/trunk/src/utest/srs_utest_protocol.cpp b/trunk/src/utest/srs_utest_protocol.cpp index 276922d89f..57d9ffe44c 100644 --- a/trunk/src/utest/srs_utest_protocol.cpp +++ b/trunk/src/utest/srs_utest_protocol.cpp @@ -399,7 +399,7 @@ VOID TEST(ProtocolHandshakeTest, VerifyFPC0C1) // manually validate the c1 // @see: calc_c1_digest char* c1s1_joined_bytes = new char[1536 -32]; - SrsAutoFreeA(char, c1s1_joined_bytes); + SrsUniquePtr cp_uptr(c1s1_joined_bytes); HELPER_ASSERT_SUCCESS( c1.payload->copy_to(&c1, c1s1_joined_bytes, 1536 - 32, false)); bool is_valid; @@ -3355,14 +3355,16 @@ VOID TEST(ProtocolStackTest, ProtocolAckSizeFlow) } // recv auto send acked size. #1 if (true) { - SrsCommonMessage* msg = NULL; SrsUniquePtr msg_uptr(msg); + SrsCommonMessage* msg = NULL; HELPER_ASSERT_SUCCESS(proto.recv_message(&msg)); + SrsUniquePtr msg_uptr(msg); ASSERT_TRUE(msg->header.is_ackledgement()); } // send again if (true) { - SrsCommonMessage* msg = new SrsCommonMessage(); SrsUniquePtr msg_uptr(msg); + SrsCommonMessage* msg = new SrsCommonMessage(); + SrsUniquePtr msg_uptr(msg); msg->header.payload_length = msg->size = 4096; msg->payload = new char[msg->size]; diff --git a/trunk/src/utest/srs_utest_rtmp.cpp b/trunk/src/utest/srs_utest_rtmp.cpp index edebab44b6..aa9412c9d6 100644 --- a/trunk/src/utest/srs_utest_rtmp.cpp +++ b/trunk/src/utest/srs_utest_rtmp.cpp @@ -2319,7 +2319,7 @@ VOID TEST(ProtocolRTMPTest, CoverAllUnmarshal) int nn = name->total_size() + arr->total_size(); char* b = new char[nn]; - SrsAutoFreeA(char, b); + SrsUniquePtr cp_uptr(b); SrsBuffer buf(b, nn); HELPER_ASSERT_SUCCESS(name->write(&buf)); @@ -2396,7 +2396,7 @@ VOID TEST(ProtocolRTMPTest, CoverAllUnmarshal) int nn = name->total_size() + tid->total_size() + null->total_size() + is_pause->total_size() + ts->total_size(); char* b = new char[nn]; - SrsAutoFreeA(char, b); + SrsUniquePtr cp_uptr(b); SrsBuffer buf(b, nn); HELPER_ASSERT_SUCCESS(name->write(&buf)); @@ -2426,7 +2426,7 @@ VOID TEST(ProtocolRTMPTest, CoverAllUnmarshal) int nn = name->total_size() + tid->total_size() + null->total_size() + stream_name->total_size() + start->total_size() + duration->total_size() + reset->total_size(); char* b = new char[nn]; - SrsAutoFreeA(char, b); + SrsUniquePtr cp_uptr(b); SrsBuffer buf(b, nn); HELPER_ASSERT_SUCCESS(name->write(&buf)); @@ -2460,7 +2460,7 @@ VOID TEST(ProtocolRTMPTest, CoverAllUnmarshal) int nn = name->total_size() + tid->total_size() + null->total_size() + stream_name->total_size() + start->total_size() + duration->total_size() + reset->total_size(); char* b = new char[nn]; - SrsAutoFreeA(char, b); + SrsUniquePtr cp_uptr(b); SrsBuffer buf(b, nn); HELPER_ASSERT_SUCCESS(name->write(&buf)); @@ -2494,7 +2494,7 @@ VOID TEST(ProtocolRTMPTest, CoverAllUnmarshal) int nn = name->total_size() + tid->total_size() + null->total_size() + stream_name->total_size() + start->total_size() + duration->total_size() + reset->total_size(); char* b = new char[nn]; - SrsAutoFreeA(char, b); + SrsUniquePtr cp_uptr(b); SrsBuffer buf(b, nn); HELPER_ASSERT_SUCCESS(name->write(&buf));