title: PHP Extensions - RETURN_STRING
body: <p>In a PHP extension, what is the difference between this:</p>
  
  <pre><code>PHP_METHOD(TestExtension, test)
  {
    MyClass *myclass;
    MyClass_Object *obj = (MyClass_Object*)zend_object_store_get_object(getThis() TSRMLS_CC);
    myclass = obj-&gt;myclass;
    if (myclass != NULL) 
    {
      string retval = myclass-&gt;test();
      RETURN_STRING(retval.c_str(), 1);
    }
    RETURN_NULL();
  }
  </code></pre>
  
  <p>and this:</p>
  
  <pre><code>PHP_METHOD(TestExtension, test)
  {
    MyClass *myclass;
    MyClass_Object *obj = (MyClass_Object*)zend_object_store_get_object(getThis() TSRMLS_CC);
    myclass = obj-&gt;myclass;
    if (myclass != NULL) 
    {
      RETURN_STRING(myclass-&gt;test().c_str(), 1);
    }
    RETURN_NULL();
  }
  </code></pre>
  
  <p>?</p>
  
  <p>Both <em>seem</em> to work, but when I run valgrind with:</p>
  
  <pre><code>valgrind --tool=memcheck --num-callers=30 --log-file=./php.log /usr/bin/php test.php
  </code></pre>
  
  <p>where test.php is:</p>
  
  <pre><code>&lt;?php
    $obj = new TestExtension("testing");
    echo $obj-&gt;test() . "\n";
  ?&gt;
  </code></pre>
  
  <p>then the latter gives me a whole bunch of errors, all of which are:</p>
  
  <pre><code>Address 0xe4c3e98 is 24 bytes inside a block of size 66 free'd
  </code></pre>
  
  <p>The valgrind summary is as follows:</p>
  
  <pre><code>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)
  </code></pre>