winlin

for #515, use SrsAutoFreeA to free the array.

... ... @@ -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);
... ...
... ... @@ -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) {
... ...
... ... @@ -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;
... ...
... ... @@ -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()) {
... ...
... ... @@ -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();
... ...
... ... @@ -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;
}
... ...
... ... @@ -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;
}
... ...
... ... @@ -31,28 +31,41 @@ 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() {
... ... @@ -60,7 +73,11 @@ public:
return;
}
delete *ptr;
if (is_array) {
delete[] *ptr;
} else {
delete *ptr;
}
*ptr = NULL;
}
... ...
... ... @@ -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()) {
... ...
... ... @@ -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();
... ...
... ... @@ -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;
... ...
... ... @@ -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;
}
... ...
... ... @@ -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) {
... ...