rimmartin / pion-ng

Pion Network Library (Boost licensed open source)
Boost Software License 1.0
1 stars 2 forks source link

Memory leak in http writer's binary data cache #11

Closed trueqbit closed 4 years ago

trueqbit commented 4 years ago

It has just come to my attention that @flatline opened a pull request, which fixes a memory leak.

The problem is that binary_cache_t derives from std::vector, but doesn't overwrite clear(). I would simply solve it differently than flatline: Turning the derivation into an implementation detail (protected/private derivation) and providing clear() is nice and clean:

     /// used to cache binary data included within the payload content
-    class binary_cache_t : public std::vector<std::pair<const char *, size_t> > {
+    class binary_cache_t : protected std::vector<std::pair<const char *, size_t> > {
     public:
         ~binary_cache_t() {
             for (iterator i=begin(); i!=end(); ++i) {
                 delete[] i->first;
             }
         }
+        void clear() {
+            for (iterator i = begin(); i != end(); ++i) {
+                delete[] i->first;
+            }
+            std::vector<std::pair<const char*, size_t> >::clear();
+        }
         inline boost::asio::const_buffer add(const void *ptr, const size_t size) {
             char *data_ptr = new char[size];
             memcpy(data_ptr, ptr, size);
         }
     };