ZF2-273: use weakref will lose the listener object

Issue Type: Bug Created: 2012-04-20T05:28:26.000+0000 Last Updated: 2012-06-14T20:41:38.000+0000 Status: Resolved Fix version(s): Reporter: simon liu (simonliu) Assignee: Ralph Schindler (ralph) Tags: - Zend\EventManager

  • callback
  • event
  • weakref

Related issues: Attachments:


PHP Warning: call_user_func() expects parameter 1 to be a valid callback, first array member is not a valid class name or object in /var/www/html/zf2-demo/vendor/ZendFramework/library/Zend/EventManager/EventManager.php on line 441

I check the code, when some event is triggered, event manager try to fetch callback object from a weakref object, the "real" callback object has been disposed, so event manager got null.

here is the code when some listeners is being attached, a listener object is created and pass to attach():

<pre class="highlight">
$moduleAutoloader = new ModuleAutoloader($options->getModulePaths());
$this->listeners[] = $events->attach('loadModules.pre', array($moduleAutoloader, 'register'), 1000);
// $moduleAutoloader will be disposed at the end of the function if no other hard reference point it.

here is the code in attach() method, a CallbackHandler is created with $callback as its argument:

<pre class="highlight">
public function attach($event, $callback = null, $priority = 1)
    // ......
    $listener = new CallbackHandler($callback, array('event' => $event, 'priority' => $priority));
    // ......

here is the code when a CallbackHandler is being constructed, only weakref object is keeped. When we retrive "real" object from weakref, if "real" object has been disposed, we got null. I think we do not expect this.

<pre class="highlight">
// We have an array callback with an object as the first argument;
// pass it to WeakRef, and then register the new callback
$target = new WeakRef($target);
$this->callback = array($target, $method);


Posted by simon liu (simonliu) on 2012-05-16T02:23:15.000+0000

I fix it, pull request is here:

Posted by Maks 3w (maks3w) on 2012-06-14T20:41:38.000+0000

PR merged

Have you found an issue?

See the Overview section for more details.


© 2006-2018 by Zend, a Rogue Wave Company. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.