Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:99257 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 11692 invoked from network); 30 May 2017 08:23:51 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 30 May 2017 08:23:51 -0000 Authentication-Results: pb1.pair.com header.from=nikita.ppv@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=nikita.ppv@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.214.47 as permitted sender) X-PHP-List-Original-Sender: nikita.ppv@gmail.com X-Host-Fingerprint: 209.85.214.47 mail-it0-f47.google.com Received: from [209.85.214.47] ([209.85.214.47:36082] helo=mail-it0-f47.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 36/FD-34073-61C2D295 for ; Tue, 30 May 2017 04:23:50 -0400 Received: by mail-it0-f47.google.com with SMTP id o5so37580301ith.1 for ; Tue, 30 May 2017 01:23:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=e0oME1/r5jwTRWVumufEOhpO8YrGgxI5WeauGEvXPpA=; b=eedttNFBIe4XkA6ig3Js3uJwwZkcW3Jl3BMvStl/IPO4rJ9Vh2l8LSUOBfUxRjQHAG WiFASGJk4bwlQCkZPXpfA9Uy9enDmBgMis7mECj22Y/isN6UByZC1E8m0ROykQfW134R ONjQhJQ/SsAEtsFc23hItqDbtKdKUwra8eRO/Wwv0cCzC4lJ8/wPaO/SU2XzrLQ7tlYq En+WpfZ9RRDiw0URwQzeWZ2xZsfmNTW8nhS2lwLn1ptlZhKV/4wo2X5IpXSR27grE5x1 g6x130FAD5xD2kcIXEWZ3Y/p2GJlJNSfFLbfy6psobC9Ad56+rF/dp8hujoDR/LAhbMR 9eGQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=e0oME1/r5jwTRWVumufEOhpO8YrGgxI5WeauGEvXPpA=; b=Q7DINvdlDm7wSwTGgHrCc2q5m5PoaD6hqEiUsgis66Qb0LTa2B8IkPUCsEYC5vHmtQ fCITfY0/V+t4vw9EHd65AFr9OukwjI1vHVt/uEtkA6PMdNkc/+WYiIEKfsUVv8YWs5Pe he/SI88O17aUy+lC7WpdTQVNTXNa2v8hXFtZxupWo9CBhGRxW+5PBDAIWZ/UZ9FUbZAH SbwJUDewt4bCuvnqRj5lJQjkAdPhU2qbYhB/bWVcFVohQABxgletFXzBNsHfqPRHRPhN edm5Xr4M2MrPZ4R5S0oKXMMUnpslj5m7iYXvJuIbxI1MC3kgfxbUIaxMMnO7ZC9aOhdV 86nA== X-Gm-Message-State: AODbwcC3UEkliwQaPfGNtnJOC8te7pVC/evWN+CDTsgFOtVvQH7giliG OWn8gkuhmQNgO92qXoktI6e2Wb6l3w== X-Received: by 10.36.192.195 with SMTP id u186mr751590itf.88.1496132627454; Tue, 30 May 2017 01:23:47 -0700 (PDT) MIME-Version: 1.0 Received: by 10.107.12.158 with HTTP; Tue, 30 May 2017 01:23:46 -0700 (PDT) In-Reply-To: <6b3bd196-d3f4-f981-9d33-47eed4f201c8@list.imperialat.at> References: <6b3bd196-d3f4-f981-9d33-47eed4f201c8@list.imperialat.at> Date: Tue, 30 May 2017 10:23:46 +0200 Message-ID: To: Martijn van Duren Cc: PHP Internals Content-Type: multipart/alternative; boundary="94eb2c05ea58cbe4840550b98501" Subject: Re: [PHP-DEV] Get variable refcount From: nikita.ppv@gmail.com (Nikita Popov) --94eb2c05ea58cbe4840550b98501 Content-Type: text/plain; charset="UTF-8" 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.) From your description, it sounds like you might be looking for the weakref PECL extension. Nikita --94eb2c05ea58cbe4840550b98501--