Hi Anatol,
On 24 March 2013 19:23, Anatol Belski <[email protected]> wrote: > Hi, > > On Sun, 2013-03-24 at 19:33 -0300, Cesar D. Rodas wrote: >> Hi there, >> >> I'm writing an extension (it'll be opensource), something inspired in >> Python's ctypes (similar to http://pecl.php.net/package/ffi) >> >> In order to make things a bit interesting, I allow to register >> resources types from the userland (I don't know if this ok), and its >> destructor is a function callback from the userland. >> >> More or less like this, >> >> <?php >> use Ctypes\Resource; >> use Ctypes\Library; >> use CTypes\Type; >> >> // Create new Resource Type >> $memory = new Resource("memory"); >> >> // load library >> $lib = new Library("/lib64/libc.so.6"); >> >> // create function proxies >> $malloc = $lib->getFunction("malloc", /* return */ $memory, /* >> prototype */ array(Type::tInteger)); >> $free = $lib->getFunction("free", NULL, array($memory)); >> >> // tell how to destroy the $memory resouces >> $memory->setDestructor($free); >> >> $memory = $malloc(250); /* do the malloc */ >> >> unset($memory); // This will call the $free. >> ?> >> >> Anyways, I'm having issues while I'm destroying the objects. It >> usually works, unless the program ends with an exception, then for >> some reason, my callback is released before the resources (according >> to valgrind, `zend_hash_graceful_reverse_destroy` is destroying my >> resources). That's what I think is the issue but it could be another >> thing also, I'm just a C wannabe developer :-) >> > > I think there is a term "resource" which is misunderstood, as what you > mean by that is the Resource class. PHP resource type is something else. I register a new resource type, anyway is not a bad idea to wrap resources inside classes/objects. > Do you define the create/destroy handlers for the Resource class? If you > save your props into the internal object struct, you can free them in > the destroy handler. See > http://lxr.php.net/xref/PHP_5_5/ext/com_dotnet/com_handlers.c#665 or > find more on lxr.php.net. Generally create_object handler should have a > destroy handler. > >> My question is if there is there a way to release (zval *) a RSHUTDOWN >> if it wasn't released yet? I could also call to Z_ADDREF_P to the >> resources when I create them, but that would mean that all resources >> will be destroyed at RSHUTDOWN, that won't be good for cli apps. >> > Do you mean long running CLI apps? Almost hard to imagine the situation > without looking at the code. Using create/destroy handlers would work > with GC or if explicit unset() was invoked. The code is not well tested, but I published a preview here (don't expect a well write thing) https://github.com/crodas/demo-ctypes And here is the memory issue https://gist.github.com/crodas/65bc7e346840da2089cf (Valgrind's output of https://github.com/crodas/demo-ctypes/blob/master/tests/memory_isses.phpt) I'll keep digging for what I'm doing right, any help/feedback is more than welcome :-) > Regards > > Anatol > > > > -- Cesar D. Rodas Open source developer http://cesarrodas.com/ Phone: +595-983-161-124 -- PECL development discussion Mailing List (http://pecl.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
