title: PHP 7 return dynamic alloc zval right way
body: <p>what is actually the right way to return a dynamic allocated zval from a c function, and register it's automatic dtor?</p>
  
  <pre><code>zval * php_test_fcn() {
      zval * t;
      t = ecalloc(sizeof(zval), 1);
      ZVAL_LONG(t, 1);
      return t;
  }
  </code></pre>
  
  <p>And on the other side I call:</p>
  
  <pre><code>zval **params = NULL;
  int arg_num=5;
  int x=0;
  params = ecalloc(arg_num, sizeof(zval));
  for(x=0; x&lt;arg_num; x++) {
      params[i]=php_test_fcn();
  }
  
  ///SOME MORE code
  /// call_user_function .....
  call_user_function(EG(function_table), NULL, func, &amp;return_value, arg_num, *params TSRMLS_CC);
  //cleanup
  for(x=0; x&lt;arg_num; x++) {
      zval_ptr_dtor(params[i]);
  }
  efree(params);     
  </code></pre>
  
  <p>Before PHP 7 I used: <code>MAKE_STD_ZVAL()</code> which seemed to register an auto-dtor.
  right now valgrind shows that the <code>ecalloc</code> from <code>php_test_fcn()</code> leaks.</p>
  
  <p>Any advice?</p>
  
  <p>p.s.: the code is extracted from a project, and maybe not 100% right - i tried to minify it.</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>The ecalloc on params - is the actual leak.</p>
  
  <p>If I do it: </p>
  
  <pre><code>zval params[100];
  int  i      = 0;
  int  arg_num  = lua_gettop(L);
  
  //params = ecalloc(arg_num, sizeof(zval));
  if(arg_num &gt;= 100) return 0;
  
  for (i=0; i&lt;arg_num; i++) {     
      //params[i] = php_lua_get_zval_from_lua(L, -(arg_num-i), NULL TSRMLS_CC);
      ZVAL_COPY_VALUE(&amp;params[i], php_lua_get_zval_from_lua(L, -(arg_num-i), NULL TSRMLS_CC));
  
  }
  
  call_user_function(EG(function_table), NULL, func, &amp;return_value, arg_num, params TSRMLS_CC);
  php_lua_send_zval_to_lua(L, &amp;return_value TSRMLS_CC);
  
  for (i=0; i&lt;arg_num; i++) {
      zval_ptr_dtor(&amp;params[i]);
  }
  
  //efree(params);
  zval_ptr_dtor(&amp;return_value);
  </code></pre>
  
  <p>It doesn't leak - coz of local variables.
  any idea whats wrong with the ecalloc on the <code>**</code> pointer?</p>