[Gambas-devel] Store GB_VARIANT in memory
Tobias Boege
tobias at ...692...
Tue Aug 14 16:10:25 CEST 2012
On Tue, 14 Aug 2012, Beno?t Minisini wrote:
> > BEGIN_METHOD(Deque_PushFront, GB_VARIANT value)
> >
> > void *newval;
> >
> > GB.Alloc(&newval, sizeof(GB_VARIANT_VALUE));
> > GB.StoreVariant(ARG(value), newval);
> > CDEQUE_push_front(THIS, newval); // Just links the copy to the list
> >
> > END_METHOD
> >
> > BEGIN_METHOD_VOID(Deque_PopFront)
> >
> > void *value;
> >
> > if (CDEQUE_is_empty(THIS)) {
> > GB.ReturnNull();
> > return;
> > }
> > value = CDEQUE_pop_front(THIS); // Retrieves newval from above and
> > // deletes the list node
> > GB.ReturnVariant(value);
> > GB.Free(&value);
> >
> > END_METHOD
> >
> > (Don't mind the names ;-))
> >
>
> Too much english for me in one sole mail. :-)
>
You haven't had to write it! ;-)
> Did you look at how the Collection class has been implemented?
>
> --> /main/gbx/gbx_c_collection.c
>
> Or simpler the Variant[] array?
>
> --> /main/gbx/gbx_c_array.c
>
The latter, yes. Until this minute, I looked again and saw that all Arrays
simply hold the memory they once occupied - kind of optimisation, I think,
and necessary as you said below...
> They both store and retrieve variants. Or you can look at how the
> Control.Tag property in gb.qt4, it's a variant stored inside a
> dynamically allocated object.
>
> --> /gb.qt4/src/CWidget.cpp
>
> If the Variant is not global, but stored inside a dynamically allocated
> C structure, nothing changes:
I'll have a look at the other two then.
>
> --8<-------------------------------------------------------
>
> // My object
>
> typedef
> struct {
> GB_BASE ob;
> GB_VARIANT_VALUE tag;
> }
> MYOBJECT;
>
> #define THIS ((MYOBJECT *)_object)
>
> BEGIN_METHOD(Store, GB_VARIANT value)
>
> GB.StoreVariant(ARG(value), &THIS->tag);
>
> END_METHOD
>
> BEGIN_METHOD_VOID(Restore)
>
> // The variant is copied in a temporary place.
> GB.ReturnVariant(&THIS->tag);
>
> // Free the stored variant.
> // But you can't do it now, because the Variant will
> // be referenced after the function has returned only.
> // GB.StoreVariant(NULL, &THIS->tag);
>
> END_METHOD
>
> --8<-------------------------------------------------------
>
> Regards,
>
> --
> Beno?t Minisini
>
_This_ is confusing and may be the error in my code. I thought, the Variant
is copied to a temporary place by GB.Return*() (AFAICS this "TEMP" variable.
Haven't I once heard that Gambas is not thread-safe? :-))
Why can't I free up my storage of the Variant?
Maybe my approach is just too different from what you proposed... What comes
to my mind now is this: GB.StoreVariant() refs the Object it takes but it
gets never unref'd by my code. If this is true, no wonder why I had memory
errors...
I can (and actually wanted to) rewrite the code a bit so I can test your
suggestion - which is not to try to free the buffer but instead keep it and
re-use like the Array classes do. We'll see... Thanks.
Regards,
Tobi
More information about the Devel
mailing list