Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:99259 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 15572 invoked from network); 30 May 2017 09:06:12 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 30 May 2017 09:06:12 -0000 Authentication-Results: pb1.pair.com smtp.mail=php@list.imperialat.at; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=php@list.imperialat.at; sender-id=pass Received-SPF: pass (pb1.pair.com: domain list.imperialat.at designates 80.101.55.235 as permitted sender) X-PHP-List-Original-Sender: php@list.imperialat.at X-Host-Fingerprint: 80.101.55.235 mail.imperialat.at Received: from [80.101.55.235] ([80.101.55.235:58749] helo=mail.imperialat.at) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id C2/AE-34073-3063D295 for ; Tue, 30 May 2017 05:06:12 -0400 Received: from mail.imperialat.at (localhost [127.0.0.1]) by mail.imperialat.at (OpenSMTPD) with ESMTP id 16960b4b; Tue, 30 May 2017 11:06:08 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d= list.imperialat.at; h=subject:to:cc:references:from:message-id :date:mime-version:in-reply-to:content-type :content-transfer-encoding; s=deathstar; bh=40eduC34AT+pjYzn7A8J PhWNdZ5zpI8aALso6lh4Ej0=; b=tmQJmYt5CXVS9ktgQ/18Bv3MM80XY4hQUjlG jzHa7psJfToEsCRTsp82chRkW1kwMpwT1eAcyDeRBUuLvv+G48zaIorX2IcQhz+g dXT1h+EP1+rMwMixt2+zurDs6eVU4o9xceK+r0flj+x0ZTBlaww1iaeqf7B1awX0 PWbK120R6C7t9XaKUVSd+qBEhxZ57C9AKcXBH6pzyqWh6D7m0N2hfduBCy/hpwLE 4ybaH0pJsexr4J42Gbtazz9EgdA1/pfIYe/Rvgh/T6eL+Udlk2ry04WZ0ZfxRAE0 8mJtuXlyUUGMO18qbpXrLSgE0bSUTbevQPXmjt7HOpow1eYYjg== Received: from harrypotter.imperialat.at (192.168.153.157 [192.168.153.157]) by mail.imperialat.at (OpenSMTPD) with ESMTPSA id 3633a766 (TLSv1.2:ECDHE-RSA-CHACHA20-POLY1305:256:NO); Tue, 30 May 2017 11:06:08 +0200 (CEST) To: Nikita Popov Cc: PHP Internals References: <6b3bd196-d3f4-f981-9d33-47eed4f201c8@list.imperialat.at> Message-ID: Date: Tue, 30 May 2017 11:06:08 +0200 User-Agent: Mozilla/5.0 (X11; OpenBSD amd64; rv:52.0) Gecko/20100101 Thunderbird/52.1.1 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Language: en-GB Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] Get variable refcount From: php@list.imperialat.at (Martijn van Duren) On 05/30/17 10:23, Nikita Popov wrote: > On Tue, May 30, 2017 at 9:11 AM, Martijn van Duren > wrote: > >> Hello internals@, >> >> I'm new to this list, so let me first introduce myself. >> My name is Martijn van Duren and I work for a webhosting company in the >> Netherlands. Apart from that I'm also an OpenBSD h^Hslacker. >> >> tl;dr: >> How do I properly use Z_REFCOUNT on a zval? >> >> I'm faced with the following issue: >> I've build a framework which allows to talk to a remote interface over >> a persistent connection. This interface can return variables of >> arbitrary types. When an object is returned it is stored locally in an >> array accompanied by its id. This way I can ensure that the same object >> (and not an identical object) is returned on multiple requests. >> >> The problem I'm facing is that because this interface holds a reference >> of the object it's never truly released and thus a memory leak. For the >> application I use it for this is not an issue, but that might change in >> the future. >> >> To solve this I was thinking of creating a small extension which exports >> a function that gives me the active reference count. This way I could >> check in certain parts of the code if there's more than 1 variables >> linked to the object and if not, remove it from the internal array. >> This would not give a 100% result, but it's still better than hanging on >> to everything all the time. >> >> When playing with the extension I made the following function: >> PHP_FUNCTION(refcount) >> { >> zval var; >> zend_long refcount; >> >> if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &var) == FAILURE) { >> return; >> } >> >> refcount = (zend_long) Z_REFCOUNT(var); >> >> RETURN_LONG(refcount); >> } >> >> Which didn't returned what I expected: >> $ php -r 'dl("refcount.so"); for ($a = 0; $a < 4; $a++) >> echo refcount($a)."\n";' >> 0 >> 1 >> 2 >> 3 >> >> Could someone point me to what I'm doing wrong with Z_REFCOUNT? >> If there is a better way to solve my actual problem I would be all ears >> of course. >> >> Sincerely, >> >> Martijn van Duren >> > > Since PHP 7 not all zval types are refcounted. Before accessing Z_REFCOUNT > you should first use Z_REFCOUNTED to check if a refcount exists. You are > testing with integers, which is one of the not reference counted types. > (Objects are always reference counted.) Thanks for the explanation. > > From your description, it sounds like you might be looking for the weakref > PECL extension. That's even better than my original concept. Thanks. > > Nikita >