Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:99255 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 5500 invoked from network); 30 May 2017 07:11:53 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 30 May 2017 07:11:53 -0000 Authentication-Results: pb1.pair.com header.from=php@list.imperialat.at; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=php@list.imperialat.at; spf=pass; 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:59462] helo=mail.imperialat.at) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id AD/3D-34073-73B1D295 for ; Tue, 30 May 2017 03:11:52 -0400 Received: from mail.imperialat.at (localhost [127.0.0.1]) by mail.imperialat.at (OpenSMTPD) with ESMTP id 2933b14e for ; Tue, 30 May 2017 09:11:48 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d= list.imperialat.at; h=to:from:subject:message-id:date :mime-version:content-type:content-transfer-encoding; s= deathstar; bh=9pnoS+ulu1wYVQb6mm/TCcajXRQJyYKGMEeg9XrgRd0=; b=ew gt40uDf3b/fDc4RMj7cn/l/S/mMc/+9vg64T0c3tGQ8pubKLqFyduTepXMxHynNa V2n6nslWCPks/JhOuQrRZbPhCkUgi3k1r9OZqGE0EGul+3HF5T2gfWvRlG2vRUxc iq1zQsYcAX+jmvg6ErYGYzYHIlNTULYxpR/RcXRQ7KLsM5q5vw37nTATzUKzkG50 kdxR4p8LiiVMgBwsYopw6n4CG12I7IMwd4BLbbS9LW3sFRpkGZoU5zCDIgpcwfCW M3crMR4WP0YvZ1oAiDFLlUZBA/5V4TlbbysMG7WGjZASVUIzkC9cglaXPklLk2Y2 +sbO9tduVPNw0pOrOkxg== Received: from harrypotter.imperialat.at (192.168.153.157 [192.168.153.157]) by mail.imperialat.at (OpenSMTPD) with ESMTPSA id 6016f6f9 (TLSv1.2:ECDHE-RSA-CHACHA20-POLY1305:256:NO) for ; Tue, 30 May 2017 09:11:48 +0200 (CEST) To: PHP Internals Message-ID: <6b3bd196-d3f4-f981-9d33-47eed4f201c8@list.imperialat.at> Date: Tue, 30 May 2017 09:11:47 +0200 User-Agent: Mozilla/5.0 (X11; OpenBSD amd64; rv:52.0) Gecko/20100101 Thunderbird/52.1.1 MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Language: en-GB Content-Transfer-Encoding: 7bit Subject: Get variable refcount From: php@list.imperialat.at (Martijn van Duren) 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