Leviathan has asked for the wisdom of the Perl Monks concerning the following question:
Fellow monks,
I'm writing bindings for etk, a graphical toolkit using the Enlightenment Foundation Libraries (http://www.enlightenment.org/). A beta version of which is now at CPAN - see Etk.
Currently, when an Etk_Widget is passed around, through the typemap a call to create the appropriate SV is done. This lead to some issues with memory usage, so I thought about making an object cache in which to store created SVs and using them when an SV for a certain object is requested. Sounds simple in principle.
To do this I created a global HV that gets initialized when the library is loaded and freed when it is unloaded, and SVs are stored in this hash where the keys are the pointer values of the Etk_Widgets requested.
The problem lies in freeing these entries; the first road I took is to create this XS code:
void DESTROY(object) SV * object CODE: FreeEtkObject(object);
Where FreeEtkObject releases this object from the object cache and then frees it. But this leads to the obvious problems in such code:
my $button = Etk::Button->new(); $button->LabelSet("moo"); $button->SignalConnect("clicked", sub { my $self = shift; print $self->LabelGet(),"\n"; });
In the anonymous sub, $self is a reference to the button, so when requested, it will be taken from the object cache directly, but when the sub ends, $self is destroyed and thus it is freed from the object cache, and all sorts of havoc ensues.
Now, if I don't free the objects with DESTROY, but instead just remove them from the object cache, it makes the object cache almost useless, because graphical applications tend to contain a lot of scopes, so creating and deleting entries will probably make the cache slow down the process.
My question is, how one would go about doing such a cache, or more importantly, is this where I should be looking for a solution or am I trying to solve the wrong problem? Should I be implementing reference counting on the cache? Any obvious roads that I'm missing?
Thanx in advance.
--
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Scope, Reference Counting, Mortality... a question about Object Caching
by chromatic (Archbishop) on Aug 11, 2006 at 18:48 UTC | |
|
Re: Scope, Reference Counting, Mortality... a question about Object Caching
by cdarke (Prior) on Aug 11, 2006 at 14:17 UTC | |
by Leviathan (Scribe) on Aug 11, 2006 at 14:42 UTC |