whether to use zval_ptr_dtor or FREE_ZVAL

1.1k views Asked by At

I use the zval type in my code, if i use the zval_ptr_dtor to free the memory , it will be the result below. When i use the gdb to debug it , I confuse about the why this error show up.

/vagrant_data/php-5.6.25/Zend/zend_execute.h(79) : Block 0x7ffff063f2b3 status:
/vagrant_data/php-5.6.25/Zend/zend_variables.c(37) : Actual location (location was relayed)
Invalid pointer: ((thread_id=0x00000000) != (expected=0xF7FF0700))

Program received signal SIGSEGV, Segmentation fault.
0x000000000064bb28 in zend_mm_check_ptr (heap=0xb6a180, ptr=0x7ffff063f2b3, silent=0, __zend_filename=0x8f1e78 "/vagrant_data/php-5.6.25/Zend/zend_execute.h", __zend_lineno=79, 
    __zend_orig_filename=0x8f4a00 "/vagrant_data/php-5.6.25/Zend/zend_variables.c", __zend_orig_lineno=37) at /vagrant_data/php-5.6.25/Zend/zend_alloc.c:1384
1384        if (p->info._size != ZEND_MM_NEXT_BLOCK(p)->info._prev) {

here is my code

zval *z;
MAKE_STD_ZVAL(z);
ZVAL_STRING(z, "this is a test", 0);
zval_set_refcount_p(z, 1);
zval_ptr_dtor(&z);
1

There are 1 answers

0
somnium On

See http://www.phpinternalsbook.com/zvals/memory_management.html

zval_ptr_dtor takes a zval**, but you are handing in a zval*. Use zval_dtor.

Generally speaking you should always use zval_ptr_dtor or zval_dtor as they are checking the refcounter while FREE_ZVAL weill efree (PHP's version of free) the variable regardless of reference count. Therefore if someone else holds a reference you will run into a use-after-free.