Memory leak using malloc in php extension

I made a php extension that looks like this:

PHP_FUNCTION(function_name)
{
    ...
    proc_data = ( char * )malloc(length);
    ...
    RETURN_STRINGL( proc_data, length, 1 );

}

function_name is a function that will be available in PHP code and returns a string. But this line is allocated with malloc memory, it will be automatically freed or I have to do something. I know emalloc, but what's the difference if I use it?

Is there a better way to do this?

+3
source share
1 answer

emalloc()php uses its own memory allocator (which is optimized for php workload and ensures maximum memory usage).

emalloc(), , PHP .

RETURN_STRINGL() , .

PHP , .

PHP . , efree(), /, emalloc().

, emalloc() 0 RETURN_STRINGL():

RETURN_STRINGL(proc_data, length, 0);

, , :

// RETVAL_STRINGL lets you set the return value, and then do anything before
// actually leaving the function
RETVAL_STRINGL(proc_data, length, 1);
free(proc_data);
return;
+5
source

All Articles