gpt4 book ai didi

php - 在 php 扩展中分配内存

转载 作者:太空宇宙 更新时间:2023-11-03 23:53:27 26 4
gpt4 key购买 nike

我不擅长 C) 中的指针

在我的 PHP 扩展中遇到内存分配问题。我正在尝试调用一个返回 float 组的函数。

我用 C 编写了一个小的测试脚本,它可以运行。

基本上是,

float *fld;
...
ier = c_fstluk(fld, key, &ni, &nj, &nk);
...
// Read the array as a 2d field
for (i=0; i<ni; i++) {
for (j=0; j<nj; j++) {
// Values come transposed..
printf("%15.6E", *(fld+(ni*j)+i));
if (j<nj-1) printf(", ");
}
printf("\n");
}

( full code )

在这里我不需要mallocfree 任何东西。 (至少我不这么认为。在这个代码和函数的fortran版本中,我需要先allocate() fld。)

但是,在我的 PHP 扩展中,相同的代码会返回段错误。

当我 emallocefree(或者只是 mallocfree)调用 c_fSTLuk 之前的 fld ,它有效,但我遇到了大量的内存错误。

[Wed Jan  9 15:34:33 2013]  Script:  '/Users/matt/aurams/trunk/web/php/php-src/ext/fstd/fstd.php'
/Users/matt/aurams/trunk/web/php/php-src/Zend/zend_API.c(1295) : Freeing 0x10D953060 (72 bytes), script=/Users/matt/aurams/trunk/web/php/php-src/ext/fstd/fstd.php
/Users/matt/aurams/trunk/web/php/php-src/Zend/zend_hash.c(412) : Actual location (location was relayed)
Last leak repeated 779 times
[Wed Jan 9 15:34:33 2013] Script: '/Users/matt/aurams/trunk/web/php/php-src/ext/fstd/fstd.php'
/Users/matt/aurams/trunk/web/php/php-src/Zend/zend_API.c(1292) : Freeing 0x10D9531A0 (32 bytes), script=/Users/matt/aurams/trunk/web/php/php-src/ext/fstd/fstd.php
Last leak repeated 779 times
[Wed Jan 9 15:34:33 2013] Script: '/Users/matt/aurams/trunk/web/php/php-src/ext/fstd/fstd.php'
ext/fstd/fstd.c(414) : Freeing 0x10D9538D0 (72 bytes), script=/Users/matt/aurams/trunk/web/php/php-src/ext/fstd/fstd.php
/Users/matt/aurams/trunk/web/php/php-src/Zend/zend_API.c(982) : Actual location (location was relayed)
Last leak repeated 29 times
[Wed Jan 9 15:34:33 2013] Script: '/Users/matt/aurams/trunk/web/php/php-src/ext/fstd/fstd.php'
/Users/matt/aurams/trunk/web/php/php-src/Zend/zend_hash.c(450) : Freeing 0x10D954C08 (256 bytes), script=/Users/matt/aurams/trunk/web/php/php-src/ext/fstd/fstd.php
Last leak repeated 29 times
=== Total 1620 memory leaks detected ===

( Full code with emalloc commented out ,第 398 行)

我打赌我在这里遗漏了一些简单的东西..

总而言之,在独立的 C 程序中,东西无需任何分配即可工作。在 PHP 扩展中,它在我分配空间时工作,但抛出内存错误,当我不分配空间时,它会出现错误。

帮助?谢谢!

最佳答案

根据这个document , c_fSTLuk(buffer, key, &ni, &nj, &nk) 将数据读入buffer,因此必须分配缓冲区(fld)。

在 C 版本中,if 在未分配 fld 的情况下通常不会工作。但是取决于您的系统等...或者运气,fld * 指针可能指向未写保护的随机空间(有效,但当然,这是非常危险的)。

因此扩展还必须分配内存(永久或 PHP 请求生命仅取决于您的分机)然后释放它 - 在您的情况下它是一个临时分配,并且必须在离开 *php_function 之前释放*.

我在您提供的代码中看到的内容被注释掉了

  • emalloc 进行分配
  • 然后你强制模块返回 0 因为你想测试这部分(而不是 PHP 变量创建等...)
  • 然后efree fld
  • 因此 fld 未被释放并造成内存泄漏

所以这可能是一个简单的错误,efree 应该在函数返回之前移动。

fld = emalloc(ni*nj*sizeof(float*)); /* sizeof(float)  INSTEAD? */

// Read the field pointed by the given key
ier = c_fstluk(fld, key, &ni, &nj, &nk);

/* ... */

php_printf("\nDone.\n");

efree(fld);

RETURN_LONG(0);

根据评论进行编辑

  • (float *) 分配更改为 (float)
  • ALLOC_INIT_ZVAL(arow) 为所有迭代完成(源底部)

关于php - 在 php 扩展中分配内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14262531/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com