Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:6750 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 69791 invoked by uid 1010); 3 Jan 2004 00:01:22 -0000 Delivered-To: ezmlm-scan-internals@lists.php.net Delivered-To: ezmlm-internals@lists.php.net Received: (qmail 69745 invoked from network); 3 Jan 2004 00:01:22 -0000 Received: from unknown (HELO blacksheep.csh.rit.edu) (129.21.60.6) by pb1.pair.com with SMTP; 3 Jan 2004 00:01:22 -0000 Received: from fury.csh.rit.edu (fury.csh.rit.edu [IPv6:2001:470:1f00:135:a00:20ff:fe8d:5399]) by blacksheep.csh.rit.edu (Postfix) with ESMTP id 3F55A25B for ; Fri, 2 Jan 2004 19:01:22 -0500 (EST) Received: by fury.csh.rit.edu (Postfix, from userid 37404) id E3BEF137F; Fri, 2 Jan 2004 19:01:21 -0500 (EST) Date: Fri, 2 Jan 2004 19:01:21 -0500 To: internals@lists.php.net Message-ID: <20040103000120.GA23760@csh.rit.edu> Mail-Followup-To: internals@lists.php.net Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Subject: Custom object creation and reference counts From: jon@php.net (Jon Parise) I've been rewriting my embedded Python extension for PHP5 so that I can take advantage of ZE2's spiffy object handler system. I've just gotten most things working, but I noticed that my custom object destructor is never being executed. I ran the following through the debugger a few times: $p = new Python('foo', 'Test'); And compared it to the execution of a "native" PHP class: class Foo {} $f = new Foo(); The difference appears to lay with the reference counts on the resultant store object. With my Python class, an additional reference is added to the store object, so zend_objects_store_del_ref() never drops the reference count to zero, and the destructor is never invoked. It looks like this code from zend_execute.c is responsible for the added reference: if (PZVAL_IS_REF(value) && value->refcount > 0) { ALLOC_ZVAL(variable_ptr); *variable_ptr_ptr = variable_ptr; *variable_ptr = *value; zval_copy_ctor(variable_ptr); variable_ptr->refcount=1; break; } In my case, 'value' is a reference (PZVAL_IS_REF(value) == 1), and in the case of the native PHP object, it isn't. zval_copy_ctor() calls zend_objects_store_add_ref(), which increments the store object's reference count. Two questions: Why is my object being treated as a reference while the native PHP object is not? 'variable_ptr->refcount=1;' resets the reference count of the variable's zval to 1, but the underlying _store_object's reference count is still incremented. What will ever reclaim that reference on the _store_object? I'm still finding my way around the the "new" ZE2 way of doing things, so please be gentle if the answers to these questions is obvious. -- Jon Parise (jon@php.net) :: The PHP Project (http://www.php.net/)