|Keep It Simple, Stupid|
Do I need to free allocated space in an END sub?by shay (Beadle)
|on Dec 10, 2004 at 13:17 UTC||Need Help??|
shay has asked for the wisdom of the Perl Monks concerning the following question:
I have a perl source code filter module, written in XS/C, which contains a BOOT: section that allocates some memory using Newz() and an END subroutine that frees it using Safefree().
This works fine in "normal usage", and under CGI, but under mod_perl it breaks because the BOOT: is only run once but the END is run at the end of each request (since the filter module is necessarily pulled in during the loading of the script to be filtered, rather than preloaded via PerlModule during the server startup). Hence multiple requests end up multiply repeatedly free()'ing space that has long since already been free()'d at the end of the first request. Not good.
The question is: What can I do to solve this? Ideally I would like a solution that will work within the filter module itself, rather than requiring the users of the module to do extra stuff, and it must also work in non-mod_perl usage too.
The simplest solution is to just delete the END subroutine. The space allocated in BOOT: will get returned to the OS when the process exits anyway, so do I even need to be free()'ing it explicitly?
Can anyone see any problem with not bothering to do the free()'s at all?
Is there another solution besides just omitting the END sub? I couldn't see any counterpart to BOOT: in the perlxs manpage, and DESTROY is no good since it is only called for objects, not packages/classes.