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.

--
Leviathan.

In reply to Scope, Reference Counting, Mortality... a question about Object Caching by Leviathan

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.