diff --git a/lib/flv_tag.cpp b/lib/flv_tag.cpp
index fc8dada5..3c8a1994 100644
--- a/lib/flv_tag.cpp
+++ b/lib/flv_tag.cpp
@@ -335,6 +335,16 @@ FLV::Tag::Tag(const RTMPStream::Chunk& O){
   ChunkLoader(O);
 }
 
+/// Generic destructor that frees the allocated memory in the internal data variable, if any.
+FLV::Tag::~Tag(){
+  if (data){
+    free(data);
+    data = 0;
+    buf = 0;
+    len = 0;
+  }
+}
+
 /// Assignment operator - works exactly like the copy constructor.
 /// This operator checks for self-assignment.
 FLV::Tag & FLV::Tag::operator=(const FLV::Tag& O){
@@ -759,13 +769,14 @@ bool FLV::Tag::DTSCMetaInit(DTSC::Stream & S){
 bool FLV::Tag::ChunkLoader(const RTMPStream::Chunk& O){
   len = O.len + 15;
   if (len > 0){
-    if ( !data){
-      data = (char*)malloc(len);
-      buf = len;
-    }else{
-      if (buf < len){
-        data = (char*)realloc(data, len);
+    if (buf < len || !data){
+      char * newdata = (char*)realloc(data, len);
+      // on realloc fail, retain the old data
+      if (newdata != 0){
+        data = newdata;
         buf = len;
+      }else{
+        return false;
       }
     }
     memcpy(data + 11, &(O.data[0]), O.len);
diff --git a/lib/flv_tag.h b/lib/flv_tag.h
index b4bb5198..5b4289a5 100644
--- a/lib/flv_tag.h
+++ b/lib/flv_tag.h
@@ -39,6 +39,7 @@ namespace FLV {
       Tag(const Tag& O); ///< Copy constructor, copies the contents of an existing tag.
       Tag & operator=(const Tag& O); ///< Assignment operator - works exactly like the copy constructor.
       Tag(const RTMPStream::Chunk& O); ///<Copy constructor from a RTMP chunk.
+      ~Tag(); ///< Generic destructor.
       //loader functions
       bool ChunkLoader(const RTMPStream::Chunk& O);
       bool DTSCLoader(DTSC::Stream & S);