Re: Question
| From: | Anatol Belski | Date: | Sun, 24 Mar 2013 23:23:57 +0000 |
| Subject: | Re: Question | ||
| References: | 1 | Groups: | php.pecl.dev |
| Request: | Send a blank email to pecl-dev+get-10459@lists.php.net to get a copy of this message | ||
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.
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.
Regards
Anatol