Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
trueqbit opened this issue Mar 27, 2020 · 0 comments
Closed

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

trueqbit opened this issue Mar 27, 2020 · 0 comments

Comments

@trueqbit
Copy link
Collaborator

trueqbit commented Mar 27, 2020

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);
         }
     };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant