0
我正在实例化一个C++类作为PHP扩展中的全局。它的工作原理,然而,valgrind正在报告一个明确的内存泄漏。如何在PHP扩展中实例化一个全局C++类?
在我php_myext.h,我宣布,全球使用:
ZEND_BEGIN_MODULE_GLOBALS(myext)
MyClass *myClass;
ZEND_END_MODULE_GLOBALS(myext)
里面我PHP_MINIT_FUNCTION我设置全局初始化函数和析构函数:
ZEND_INIT_MODULE_GLOBALS(myext, myext_init_globals, myext_destroy_globals);
然后我初始化函数和析构函数被实现为如下:
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
static void myext_init_globals(zend_myext_globals *myext_globals)
{
myext_globals->myClass = new MyClass();
}
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
static void myext_destroy_globals(zend_myext_globals *myext_globals)
{
delete myext_globals->myClass;
}
我将MyClass :: test()方法暴露给PHP u唱了以下内容:
static PHP_METHOD(MyExt, test)
{
RETURN_STRING(MYEXT_G(myClass)->test().c_str(), 1);
}
一切正常,从我的PHP脚本罚款:
<?php echo MyExt::test(); ?>
然而,当我的valgrind我的测试脚本(test.php的),我得到一个泄漏:
LEAK SUMMARY:
definitely lost: 8 bytes in 1 blocks
indirectly lost: 42 bytes in 1 blocks
possibly lost: 0 bytes in 0 blocks
still reachable: 2,256 bytes in 18 blocks
suppressed: 0 bytes in 0 blocks
Reachable blocks (those to which a pointer was found) are not shown.
To see them, rerun with: --leak-check=full --show-reachable=yes
For counts of detected and suppressed errors, rerun with: -v
ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 282 from 9)
如果我使用“new”删除实例化MyClass的部件,则不存在内存泄漏。这导致我相信C++类需要使用其他方法/宏在PHP扩展中实例化?
任何帮助,揭示这一点真的很感激。