phalcon/cphalcon — pull request #1302 by sjinks (2013-09-27 20:29:07+00:00)
Title: Use preallocated permanent zvals instead of null, true, false, 0 and 1

This is what was implemented in 2.0.0 / Zephir and appears to be very useful optimization. In some cases it helps avoid creation of the memory frame because we don't need `PHALCON_INIT_VAR` anymore.

In addition, this PR fixes the bug when a function calls another function (passing it its return_value and return_value_ptr) and the called function throws an exception:

```c
PHP_FUNCTION(test1)
{
phalcon_call_function_p0_ex(return_value, return_value_ptr, "test2");
}

PHP_FUNCTION(test2)
{
zend_throw-exception(...);
}
```

In this scenario, Zend (`zend_call_function()`) detects the exception and frees the variable `return_value_ptr` [points to](http://lxr.php.net/xref/PHP_5_5/Zend/zend_execute_API.c#967). But because `test1` passed to `test2` its own `return_value`/`return_value_ptr`, `test2` frees `test1`'s return value. Then, after `test2` and `test1` return, Zend sees there is an active exception and tries to free the return value of `test1` which was already freed on return from `test2` and segmentation fault happens.

We tried to address this issue in the past (in `phalcon_alt_call_method`) but that fix is not applicable to all kinds of static calls because they are dispatched via `zend_call_function`.