在PHP擴展,也正是這一區別:和PHP擴展 - RETURN_STRING
PHP_METHOD(TestExtension, test)
{
MyClass *myclass;
MyClass_Object *obj = (MyClass_Object*)zend_object_store_get_object(getThis() TSRMLS_CC);
myclass = obj->myclass;
if (myclass != NULL)
{
string retval = myclass->test();
RETURN_STRING(retval.c_str(), 1);
}
RETURN_NULL();
}
這樣的:
PHP_METHOD(TestExtension, test)
{
MyClass *myclass;
MyClass_Object *obj = (MyClass_Object*)zend_object_store_get_object(getThis() TSRMLS_CC);
myclass = obj->myclass;
if (myclass != NULL)
{
RETURN_STRING(myclass->test().c_str(), 1);
}
RETURN_NULL();
}
?
兩個似乎工作,但是當我的valgrind運行:
valgrind --tool=memcheck --num-callers=30 --log-file=./php.log /usr/bin/php test.php
其中test.php的是:
<?php
$obj = new TestExtension("testing");
echo $obj->test() . "\n";
?>
那麼後者給了我一大堆的錯誤,所有其中有:
Address 0xe4c3e98 is 24 bytes inside a block of size 66 free'd
valgrind摘要如下:
126 ==23067== HEAP SUMMARY:
127 ==23067== in use at exit: 9,031 bytes in 15 blocks
128 ==23067== total heap usage: 25,131 allocs, 25,116 frees, 4,435,757 bytes allocated
129 ==23067==
130 ==23067== LEAK SUMMARY:
131 ==23067== definitely lost: 0 bytes in 0 blocks
132 ==23067== indirectly lost: 0 bytes in 0 blocks
133 ==23067== possibly lost: 0 bytes in 0 blocks
134 ==23067== still reachable: 9,031 bytes in 15 blocks
135 ==23067== suppressed: 0 bytes in 0 blocks
136 ==23067== Rerun with --leak-check=full to see details of leaked memory
137 ==23067==
138 ==23067== For counts of detected and suppressed errors, rerun with: -v
139 ==23067== ERROR SUMMARY: 48 errors from 5 contexts (suppressed: 0 from 0)
在RETURN_STRING()中myClass-> test()之後缺少逗號? – damndaewoo 2015-01-21 11:03:34
沒有缺失的逗號。 – 2015-01-21 11:16:29