zend_list_addref and object resources

php.internals

Marshall A. Greenblatt

22 years ago
Hi, I'm having a bit of a conundrum that maybe someone here can help me out with :-). When assigning an object resource to either 'getThis()' or 'return_value' as shown below, is it necessary to manually increment the reference count? For example, /ext/ming/ming.c (version 1.70) contains many code fragments like the following where zend_list_addref() is used with 'getThis()', and not used with 'return_value': <snip ming.c:297> ret = zend_list_insert(action, le_swfactionp); object_init_ex(getThis(), action_class_entry_ptr); add_property_resource(getThis(), "action", ret); zend_list_addref(ret); </snip> <snip ming.c:1979> ret = zend_list_insert(item, le_swfdisplayitemp); object_init_ex(return_value, displayitem_class_entry_ptr); add_property_resource(return_value, "displayitem", ret); </snip> Looking at earlier revisions of this file, it appears that usage of zend_list_addref() with 'return_value' has been steadily decreasing. There is, however, one place where zend_list_addref() with 'return_value' is still being called: <snip ming.c:1686> int ret = zend_list_insert(shape, le_swfshapep); object_init_ex(return_value, shape_class_entry_ptr); add_property_resource(return_value, "shape", ret); zend_list_addref(ret); </snip> I can't find enough examples in other modules to feel confident that zend_list_addref() with 'return_value' is a bad thing (some use it, some don't), and my testing seems to show that everything works fine with or without the explicit zend_list_addref() call for both 'return_value' and 'getThis()'. That said, is there a "correct" way to handle reference counting with 'getThis()' and 'return_value' in this situation? Thanks, Marshall

Marshall A. Greenblatt

22 years ago
----- Original Message ----- From: "Marshall A. Greenblatt" <marshall@magpcss.com> To: <internals@lists.php.net> Sent: Monday, August 09, 2004 11:38 PM Subject: [PHP-DEV] zend_list_addref and object resources [snip]
> When assigning an object resource to either 'getThis()' or 'return_value'
as
> shown below, is it necessary to manually increment the reference count? > > For example, /ext/ming/ming.c (version 1.70) contains many code fragments > like the following where zend_list_addref() is used with 'getThis()', and > not used with 'return_value': > > <snip ming.c:297> > ret = zend_list_insert(action, le_swfactionp); > > object_init_ex(getThis(), action_class_entry_ptr); > add_property_resource(getThis(), "action", ret); > zend_list_addref(ret); > </snip>
[snip] In answer to my own question: No, at least not with PHP4.3.1. <?php /* Create an object with a resource and return via 'getThis()'. */ $foo = new myobj(); print_zval($foo); /* Create an object with a resource and return via 'return_value'. */ $bar = getmyobj(); print_zval($bar); ?> In either case, irregardless of whether zend_list_addref() is explicitly called for the resource ID, output (via print_zval) is as follows: Addr:[9F75D0] is_ref:[0] refcount:[2] Type:[OBJECT] Class:[myobj] Val:[ Name:[value] => Addr:[9F7798] is_ref:[0] refcount:[1] Type:[RESOURCE] Val:[1] ] Addr:[9F7638] is_ref:[0] refcount:[2] Type:[OBJECT] Class:[myobj] Val:[ Name:[value] => Addr:[9F79B8] is_ref:[0] refcount:[1] Type:[RESOURCE] Val:[2] ] If anyone is interested in playing with this, email me and I'll make the source code available online. - Marshall Note: The print_zval function takes any type of variable and displays the value, similar to print_r, but in a more useful debugging format.