#1031 PHP wrapper leaks.

closed-accepted
Olly Betts
php (58)
5
2009-07-29
2009-07-22
Hitoshi Amano
No

The smallest code to reproduce this problem is here.

==
// example.i
%module example
%include cpointer.i
%pointer_functions(int, p)

==
<?php
// test.php
require("example.php");
example::delete_p(example::new_p());

The memory allocated by #0 is not released

#0 _emalloc (size=8, __zend_filename=0xb7fd2662 "example_wrap.c", __zend_lineno=806, __zend_orig_filename=0x0, __zend_orig_lineno=0)
at /home/amachang/Desktop/src/php-5.3.0/Zend/zend_alloc.c:2303
#1 0xb7fd18c3 in SWIG_ZTS_SetPointerZval (z=0x8b98704, ptr=0x8c0b058, type=0xb7fd3160, newobject=0) at example_wrap.c:806
#2 0xb7fd1d4f in _wrap_new_p (ht=0, return_value=0x8b98704, return_value_ptr=0x0, this_ptr=0x0, return_value_used=1) at example_wrap.c:1033
#3 0x08217f37 in zend_do_fcall_common_helper_SPEC (execute_data=0x8bca5c4) at /home/amachang/Desktop/src/php-5.3.0/Zend/zend_vm_execute.h:313
#4 0x082184f4 in ZEND_DO_FCALL_BY_NAME_SPEC_HANDLER (execute_data=0x8bca5c4) at /home/amachang/Desktop/src/php-5.3.0/Zend/zend_vm_execute.h:422
#5 0x082175d4 in execute (op_array=0x8b9a150) at /home/amachang/Desktop/src/php-5.3.0/Zend/zend_vm_execute.h:104
#6 0x081eb2a5 in zend_execute_scripts (type=8, retval=0x0, file_count=3) at /home/amachang/Desktop/src/php-5.3.0/Zend/zend.c:1188
#7 0x081844ae in php_execute_script (primary_file=0xbfff5bbc) at /home/amachang/Desktop/src/php-5.3.0/main/main.c:2196
#8 0x082ad5b6 in main (argc=4, argv=0xbfff5d14) at /home/amachang/Desktop/src/php-5.3.0/sapi/cli/php_cli.c:1188

swig_object_wrapper should be released reguardless of whether wrapped pointer is released or not.
However it is not released when the wrapped pointer is not released.

I attached patch to fix this problem.

thx

Discussion

  • Olly Betts
    Olly Betts
    2009-07-23

    Kevin's no longer active, so reassigning to myself.

     
  • Olly Betts
    Olly Betts
    2009-07-23

    • assigned_to: kruland --> olly
     
  • Olly Betts
    Olly Betts
    2009-07-29

    • status: open --> closed-accepted
     
  • Olly Betts
    Olly Betts
    2009-07-29

    Thanks for the patch, and the test case.

    The leak doesn't seem to show up with valgrind (I'm guessing PHP releases all the emalloc()-ed memory at process exit.

    But if I wrap your example with "while (true) {" ... "}" it runs out of memory really quickly, so there's clearly a leak here.

    Applied to SVN trunk as r11463.