PHP FFI - How to free memory created by FFI::new("void*[2]", false, true)

38 views Asked by At

I am using php FFI to call a C++ dll and the signature of the C++ function is:

int Do(int methid, int paramlen, void *param[], int cbparam[]);

In C++, I can call this function by

char* param1 = (char*)"test";
int param2 = 2;
void *param[2] = {(void*)param1, (void*)(unsigned long long)param2};
int cbparam[2] = {0, 0};
Do(1, 2, param, cbparam);

In PHP, I call this function through PHP FFI:

$param = $ffi->new("void*[2]", false, true);
$cbparam = $ffi->new("int[2]", false, true);  
$cstr = "test\0";

$param[0] = $ffi->new("char[" . strlen($cstr) . "]", false, true);
FFI::memcpy($param[0], $cstr, strlen($cstr));
$cbparam[0] = 0;

$param2 = 2;
$param[1] = $ffi->cast("void*", $param2);
$cbparam[1] = 0;

$ffi->Do(1, 2, $param, $cbparam);

//how to correctly free param and cbparam

I tried free $param with FFI::free($param[0]) but it throws zend_mm_heap corrupted error. If I do not free $param, I see the memory continues to grow through the PHP function var_dump(memory_get_usage());.

Add a point, The C++ function does not release this part of memory.

Thanks for your help.

0

There are 0 answers