Skip to content

Commit

Permalink
bug #15821 [EventDispatcher] fix memory leak in getListeners (Tobion)
Browse files Browse the repository at this point in the history
This PR was merged into the 2.3 branch.

Discussion
----------

[EventDispatcher] fix memory leak in getListeners

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets |
| License       | MIT
| Doc PR        |

Commits
-------

a7b7f54 [EventDispatcher] skip one lazy loading call
ec59953 [EventDispatcher] fix memory leak in a getListeners
  • Loading branch information
fabpot committed Sep 22, 2015
2 parents bd415c6 + a7b7f54 commit b2f7753
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 38 deletions.
Expand Up @@ -103,7 +103,7 @@ public function removeListener($eventName, $listener)
}

/**
* @see EventDispatcherInterface::hasListeners()
* {@inheritdoc}
*/
public function hasListeners($eventName = null)
{
Expand All @@ -119,7 +119,7 @@ public function hasListeners($eventName = null)
}

/**
* @see EventDispatcherInterface::getListeners()
* {@inheritdoc}
*/
public function getListeners($eventName = null)
{
Expand Down Expand Up @@ -155,21 +155,6 @@ public function addSubscriberService($serviceId, $class)
}
}

/**
* {@inheritdoc}
*
* Lazily loads listeners for this event from the dependency injection
* container.
*
* @throws \InvalidArgumentException if the service is not defined
*/
public function dispatch($eventName, Event $event = null)
{
$this->lazyLoad($eventName);

return parent::dispatch($eventName, $event);
}

public function getContainer()
{
return $this->container;
Expand Down
36 changes: 15 additions & 21 deletions src/Symfony/Component/EventDispatcher/EventDispatcher.php
Expand Up @@ -33,9 +33,7 @@ class EventDispatcher implements EventDispatcherInterface
private $sorted = array();

/**
* @see EventDispatcherInterface::dispatch()
*
* @api
* {@inheritdoc}
*/
public function dispatch($eventName, Event $event = null)
{
Expand All @@ -46,21 +44,23 @@ public function dispatch($eventName, Event $event = null)
$event->setDispatcher($this);
$event->setName($eventName);

if (!isset($this->listeners[$eventName])) {
return $event;
if ($listeners = $this->getListeners($eventName)) {
$this->doDispatch($listeners, $eventName, $event);
}

$this->doDispatch($this->getListeners($eventName), $eventName, $event);

return $event;
}

/**
* @see EventDispatcherInterface::getListeners()
* {@inheritdoc}
*/
public function getListeners($eventName = null)
{
if (null !== $eventName) {
if (!isset($this->listeners[$eventName])) {
return array();
}

if (!isset($this->sorted[$eventName])) {
$this->sortListeners($eventName);
}
Expand All @@ -78,17 +78,15 @@ public function getListeners($eventName = null)
}

/**
* @see EventDispatcherInterface::hasListeners()
* {@inheritdoc}
*/
public function hasListeners($eventName = null)
{
return (bool) count($this->getListeners($eventName));
}

/**
* @see EventDispatcherInterface::addListener()
*
* @api
* {@inheritdoc}
*/
public function addListener($eventName, $listener, $priority = 0)
{
Expand All @@ -97,7 +95,7 @@ public function addListener($eventName, $listener, $priority = 0)
}

/**
* @see EventDispatcherInterface::removeListener()
* {@inheritdoc}
*/
public function removeListener($eventName, $listener)
{
Expand All @@ -113,9 +111,7 @@ public function removeListener($eventName, $listener)
}

/**
* @see EventDispatcherInterface::addSubscriber()
*
* @api
* {@inheritdoc}
*/
public function addSubscriber(EventSubscriberInterface $subscriber)
{
Expand All @@ -133,7 +129,7 @@ public function addSubscriber(EventSubscriberInterface $subscriber)
}

/**
* @see EventDispatcherInterface::removeSubscriber()
* {@inheritdoc}
*/
public function removeSubscriber(EventSubscriberInterface $subscriber)
{
Expand Down Expand Up @@ -177,9 +173,7 @@ private function sortListeners($eventName)
{
$this->sorted[$eventName] = array();

if (isset($this->listeners[$eventName])) {
krsort($this->listeners[$eventName]);
$this->sorted[$eventName] = call_user_func_array('array_merge', $this->listeners[$eventName]);
}
krsort($this->listeners[$eventName]);
$this->sorted[$eventName] = call_user_func_array('array_merge', $this->listeners[$eventName]);
}
}

0 comments on commit b2f7753

Please sign in to comment.