pavelkolodin
pavelkolodin

Reputation: 2967

How to free zval in PHP5 created with MAKE_STD_ZVAL?

How to release the variable created with the following code?

zval *zval_ = nullptr;
MAKE_STD_ZVAL(zval_);
ZVAL_NULL(zval_);

Upvotes: 3

Views: 231

Answers (1)

BadZen
BadZen

Reputation: 4274

PHP has garbage-collected memory managment. You don't generally release it manually/forceably, it gets GC'd when there are no more references to it. You must, however, use references properly.

In the documentation, have a look at the Z_ADDREF, Z_DECREF, and zval_ptr_dtor. There is a Z_FREE as well, but you should not generally use this - you don't want to forceably free a zval that is referenced in other "live" (reachable) values!

For an overview of how to use the reference system with concrete examples of the corresponding plain PHP code, see the reference tutorial in the docs.

To answer your question exactly: MAKE_STD_ZVAL starts the value off with a reference count of 1. So, a single call

 zval_ptr_dtor(zval_);

will remove that reference and clear the variable at next GC iteration (and unlike Z_FREE this will do the right thing even if you pass the value to a function which might store it somewhere and increment its ref count, etc...)

Upvotes: 1

Related Questions