# Array as class member and refcounting

- Thread: https://externals.io/message/54916
- Original archive: https://news-web.php.net/php.internals/54916
- Started by Flavius Aspra on 2011-08-25T07:42:02+00:00
- Messages: 1
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #54916 — Flavius Aspra — 2011-08-25T07:42:02+00:00

URL: https://externals.io/message/54916#54916

There is something that confuses me. Let there be a class member 
Foo::$bar, which has to be initialized as an empty array in the 
constructor. If I do that (via zend_update_property), its refcount is 
increased (from 1, which it gets after alloc + array_init, to 2). It is 
obvious why this happens, since from zend_update_property gets a 
variable from somone in the outside world, and so it is right to ++ it.

But in this particular case, the array being initialized in the 
constructor, it does not need a refcount of 2, 1 is right, since it's 
used only by the object (yet).

So I thought I will Z_DELREF_P() it. And it worked. Until I started 
valgrind, which was reporting:

==4538== Invalid read of size 4
==4538==    at 0x822D3C6: _zval_ptr_dtor (zend.h:385)
==4538==    by 0x823C1FF: _zval_ptr_dtor_wrapper (zend_variables.c:189)
==4538==    by 0x824E1A1: zend_hash_destroy (zend_hash.c:529)
==4538==    by 0x826655A: zend_object_std_dtor (zend_objects.c:45)
==4538==    by 0x8266A28: zend_objects_free_object_storage 
(zend_objects.c:126)
==4538==    by 0x826C43D: zend_objects_store_del_ref_by_handle_ex 
(zend_objects_API.c:220)
==4538==    by 0x826C0AC: zend_objects_store_del_ref 
(zend_objects_API.c:172)
==4538==    by 0x823BD77: _zval_dtor_func (zend_variables.c:52)
==4538==    by 0x822B99B: _zval_dtor (zend_variables.h:35)
==4538==    by 0x822D463: _zval_ptr_dtor (zend_execute_API.c:443)
==4538==    by 0x823C1FF: _zval_ptr_dtor_wrapper (zend_variables.c:189)
==4538==    by 0x824E518: zend_hash_apply_deleter (zend_hash.c:614)
==4538==  Address 0x44c1718 is 8 bytes inside a block of size 20 free'd

So it looks like the ZE really needs the refcount to be 2. All other 
tests I've written work fine, no memleaks, no segfaults whatsoever.

Still I am a little bit confused: WHY does it need it be higher than 
(from my understanding) it should be?

Thanks,
Flavius
