与 zend 交换数据(多维数组)
我将 PHP 嵌入到我的应用程序中(用 Delphi 2010 编写),使用 PHP4Delphi 组件与 php5ts.dll 进行交互。 我想我的程序充当 PHP 的扩展(sapi 模块?),因为它注册了一些可以在 PHP 脚本中使用的函数和常量...无论如何,在使用简单数据类型时效果很好,但是当我尝试使用多维数组时作为返回值,我收到错误
模块“php5ts.dll”中地址 01CD3C35 处的访问冲突。读取地址0231E608。
堆栈列表(000A2C35){php5ts.dll} [01CD3C35] destroy_op_array + $35
(004C4D61){myApp.exe } [008C5D61] php4delphi.TPHPEngine.ShutdownEngine (Line 1497, "php4delphi.pas" + 17) + $7
php4delphi.pas 中的第 1497 行调用 tsrm_shutdown();
对我来说,垃圾收集器崩溃于脚本的末尾,所以我怀疑我没有正确地将数据发送回引擎...... 因此我的问题是如何将多维数组发送回 PHP?
我使用的模式是
var subArray: pzval;
_array_init(return_value, nil, 0);
for x := 0 to Data.Count-1 do begin
subArray := MAKE_STD_ZVAL;
_array_init(subArray, nil, 0);
// populate subarray with data, including other subarrays
...
// add subarray to the main array
add_index_zval(return_value, x, subArray);
end;
我是否必须在某个地方“注册”我创建的子数组?我是否必须增加或减少 refcount
或设置 is_ref
? IOW,子数组的 return_value 和 zvals 必须如何设置?
我尝试将 1 添加到每个数组的引用计数(尽管 MAKE_STD_ZVAL 已经将引用计数初始化为 1),这可以解决 AV 问题,但有时应用程序在执行脚本时会消失 - 我怀疑它会导致引擎内存管理器中的无限递归,从而导致 php DLL 崩溃,随身携带该应用程序... 当将 refcount 设置为 0(零;假设在 PHP 脚本中分配返回值时,它的 refcount 将为 1,然后当 PHP 变量超出范围时,它将被销毁)似乎一切正常(即没有崩溃,没有 AV) )但脚本不会生成任何输出,只是空的 html 文件...
我还将数据作为数组发送到我的函数中,然后使用 zend_hash_find, zend_hash_get_current_data
等来读取数据。这会扰乱变量的引用计数吗?即,当我完成后,我是否必须减少 zend_hash_find
返回的变量的 refcout ?
在迭代数组时重用相同的变量是否安全,即
var Val: pppzval;
new(Val);
zend_hash_internal_pointer_reset(aZendArr^.value.ht);
for x := 1 to zend_hash_num_elements(aZendArr^.value.ht) do begin
zend_hash_get_current_data(aZendArr^.value.ht, Val);
// read data from Val to local variable and do something with it
zend_hash_move_forward_ex(aZendArr^.value.ht, nil);
end;
Dispose(Val);
循环的每次迭代都应该创建/释放 Val?
TIA
艾因
I'm embeding PHP in my app (writen in Delphi 2010), using PHP4Delphi component to interface with php5ts.dll.
I guess my program acts as extension for the PHP (sapi module?) as it registers some functions and constants which can be used in PHP scripts... anyway, works well when using simple data types, but when I try to use multidimensional array as return value I get error
Access violation at address 01CD3C35 in module 'php5ts.dll'. Read of address 0231E608.
Stack list(000A2C35){php5ts.dll} [01CD3C35] destroy_op_array + $35
(004C4D61){myApp.exe } [008C5D61] php4delphi.TPHPEngine.ShutdownEngine (Line 1497, "php4delphi.pas" + 17) + $7
The line 1497 in php4delphi.pas is call to tsrm_shutdown();
To me it looks like garbage collector crashing at the end of the script, so I'm suspecting I do not send data correctly back to the engine...
thus my question is how one is supposed to send multidimensional arrays back to PHP?
The pattern I'm using is
var subArray: pzval;
_array_init(return_value, nil, 0);
for x := 0 to Data.Count-1 do begin
subArray := MAKE_STD_ZVAL;
_array_init(subArray, nil, 0);
// populate subarray with data, including other subarrays
...
// add subarray to the main array
add_index_zval(return_value, x, subArray);
end;
Do I have to somewhere "register" the subarrays I create? Do I have to increase or decrease refcount
or set is_ref
? IOW, how must the return_value and zvals of the subarrays to be set up?
I experimented with adding 1 to each array's refcount (althought MAKE_STD_ZVAL already initializes refcount to 1) and that cures the AV but then sometimes app just disappears when executing the script - I suspect it causes infinite recursion in engine's memeory manager, crashing the php DLL and taking the app with it...
When seting refcount to 0 (zero; assuming that when return value is assigned in the PHP script it's refcount will be 1 and then when the PHP variable goes out of scope it will be destroyed) all seems to work (ie no crash, no AV) but the script wont generate any output, just empty html file...
I also send data as arrays into my function, then use zend_hash_find
, zend_hash_get_current_data
etc to read the data. Could this mess up the refcounts of the variables? Ie do I have to decrease refcout of the variable returned by zend_hash_find
when I'm done with it?
And is it safe to reuse same variable when iterating over an array, ie
var Val: pppzval;
new(Val);
zend_hash_internal_pointer_reset(aZendArr^.value.ht);
for x := 1 to zend_hash_num_elements(aZendArr^.value.ht) do begin
zend_hash_get_current_data(aZendArr^.value.ht, Val);
// read data from Val to local variable and do something with it
zend_hash_move_forward_ex(aZendArr^.value.ht, nil);
end;
Dispose(Val);
or should each iteration of the loop create / free Val?
TIA
ain
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是我的工作安排:
将引用计数设置为 2 为我解决了这个问题,我不知道为什么,只是尝试了很多次,然后发现了这个。
here is my work arround:
Set the refcount to 2 solve the problem for me, I don't know why, just tried many times, and found this.
由于你的问题很长,我将把我的答案分成几个部分。
如果用字符串填充子数组,请确保 Delphi 内存管理器不会收集这些字符串。例如,您可以在将字符串添加到数组之前将其转换为 PAnsiChar
As your question quite long I will split my answer in few parts.
If you populate your subarrays with strings make sure the strings are not collected by the Delphi memory manager. For example you can convert the strings to PAnsiChar before adding it to the array