From c089aea8ebad521795610feb36bc7a937794a88c Mon Sep 17 00:00:00 2001 From: Marc Easen Date: Wed, 16 Apr 2014 13:11:40 +0100 Subject: [PATCH] Fixed a memory leak when a connection is closed the topics should also be removed if they are empty --- src/Ratchet/Wamp/TopicManager.php | 7 +++++-- tests/unit/Wamp/TopicManagerTest.php | 7 +++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/Ratchet/Wamp/TopicManager.php b/src/Ratchet/Wamp/TopicManager.php index bceee73..a69e315 100644 --- a/src/Ratchet/Wamp/TopicManager.php +++ b/src/Ratchet/Wamp/TopicManager.php @@ -77,8 +77,11 @@ class TopicManager implements WsServerInterface, WampServerInterface { public function onClose(ConnectionInterface $conn) { $this->app->onClose($conn); - foreach ($this->topicLookup as $topic) { - $topic->remove($conn); + foreach ($this->topicLookup as $topic => $storage) { + $storage->remove($conn); + if (0 === $storage->count()) { + unset($this->topicLookup[$topic]); + } } } diff --git a/tests/unit/Wamp/TopicManagerTest.php b/tests/unit/Wamp/TopicManagerTest.php index 0c31d9a..7439064 100644 --- a/tests/unit/Wamp/TopicManagerTest.php +++ b/tests/unit/Wamp/TopicManagerTest.php @@ -166,12 +166,19 @@ class TopicManagerTest extends \PHPUnit_Framework_TestCase { $method = $class->getMethod('getTopic'); $method->setAccessible(true); + $attribute = $class->getProperty('topicLookup'); + $attribute->setAccessible(true); + $topic = $method->invokeArgs($this->mngr, array($name)); + $this->assertCount(1, $attribute->getValue($this->mngr)); + $this->mngr->onSubscribe($this->conn, $name); $this->mngr->onClose($this->conn); $this->assertFalse($topic->has($this->conn)); + + $this->assertCount(0, $attribute->getValue($this->mngr)); } public function testOnErrorBubbles() {