zaphoyd/websocketpp — Issue #53: Policy-Refactor: Memory Leak in Pool
Author: megabyte1024
Created: 2012-01-20 15:13:12+00:00
State: open

file: data.hpp
issue description: There is a memory leak in the pool class due to a cyclic dependency between the pool and the data classes.
fix:

diff --git a/src/messages/data.hpp b/src/messages/data.hpp
--- a/src/messages/data.hpp
+++ b/src/messages/data.hpp
@@ -61,6 +61,7 @@ template
public:
typedef pool type;
typedef boost::shared_ptr ptr;
+	typedef boost::weak_ptr ptr_weak;
typedef typename element_type::ptr element_ptr;
typedef boost::function callback_type;

@@ -149,6 +150,7 @@ class data {
public:
typedef boost::intrusive_ptr ptr;
typedef pool::ptr pool_ptr;
+	typedef pool::ptr_weak pool_weak_ptr;

data(pool_ptr p, size_t s);

@@ -210,7 +212,10 @@ private:
if (count == 1 && s->m_live) {
// recycle if endpoint exists
s->m_live = false;
- s->m_pool->recycle(ptr(const_cast(s)));
+	pool_ptr pp = s->m_pool.lock();
+	if (pp != NULL) {
+	pp->recycle(ptr(const_cast(s)));
+	}
} else if (count == 0) {
boost::checked_delete(static_cast(s));
}
@@ -235,7 +240,7 @@ private:
// reference counting
size_t m_index;
mutable boost::detail::atomic_count m_ref_count;
- mutable pool_ptr m_pool;
+ mutable pool_weak_ptr m_pool;
mutable bool m_live;
};