# Writing a PHP extension in C++, and memory leaks

- Thread: https://externals.io/message/63005
- Original archive: https://news-web.php.net/php.internals/63005
- Started by bapluda@yahoo.com.br on 2012-09-14T11:40:13+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

---

## #63005 — bapluda@yahoo.com.br — 2012-09-14T11:40:13+00:00

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

Is it safe to write PHP extensions in C++?
I read on Sara Golemon's book that the zend engine can bailout on a request during a call to php_error_docref() or even an emaloc() that exceeds the allowed memory,  doing an immediate longjmp() to the shutdown part of the engine.
longjmp() does not work well with C++ destructors.
For example:
[code]
void some_function( TSRMLS_D )
{
 Someclass obj( 3 );
 char *dst = emalloc(Morethanscriptlimit);
 ...
 
void some_function( TSRMLS_D )
{
 Someclass obj( 3 ); 
 php_error_docref( `NULL` TSRMLS_CC, E_ERROR, "Some error" );
 ...
[/code]
in these two cases ~Someclass() may never be invoked on obj, if either php_error_docref() or emalloc calls longjmp().
