Re: string return result conventions



On Mon, Sep 15, 2008 at 3:48 PM, Owen Taylor <otaylor redhat com> wrote:
> On Mon, 2008-09-15 at 08:59 +0000, Luke Kenneth Casson Leighton wrote:
>> tim, thank you for responding.
>>
>> >> therefore it's important for me to find out what glib / gobject memory
>> >> conventions are, for strings.
>> >
>> > Strings querried through the property interfacem e.g.:
>> >
>> > gchar *string = NULL;
>> > g_object_get (label, "label", &string, NULL);
>> >
>> > is always duplicated and needs to be a freed by the caller,
>> > because the returned string might have been dynmically
>> > constructed in the objects proeprty getter (i.e. not
>> > correspond to memroy actually allocted for object member storage).
>>
>>  ok - in this situation, fortunately we have control over that.  the
>> property getter is entirely auto-generated.  the code review of the
>> new webkit glib/gobject bindings brought to light the webkit
>> convention of not imposing any "memory freeing" of e.g. strings on
>> users of the library.  use of refcounting is done on c++ objects, for
>> example.
>>
>> the strings in webkit are unicode (libicu).  _at the moment_ i'm
>> alloc-sprintf'ing strings - all of them - into utf-8 return results.
>
> Why is a sprintf involved here? g_utf16_to_utf8() will convert a UTF-16
> string into a UTF-8 string that can be freed with g_free().

owen, hi, sorry: it's a g_strdup i'm using not a sprintf.

extern "C" {
class GStringConvert {
public:
    gchar *m_str;
    GStringConvert(WebCore::String const& s) { m_str =
g_strdup(s.utf8().data()); }
    GStringConvert(WebCore::KURL const& s) { m_str =
g_strdup(s.string().utf8().data()); }
    GStringConvert(const JSC::UString & s) { m_str =
g_strdup(s.UTF8String().c_str()); }
    GStringConvert(WebCore::AtomicString const&s) { m_str =
g_strdup(s.string().utf8().data()); }
};

#define _G_GET_UTF8(str) (GStringConvert(str).m_str)
} /* extern "C" */



>> if that's not possible to do, what would you recommend, in this situation?
>
> Just return newly allocated UTF-8 strings. It's going to be a little bit
> inconvenient, with some risk of leakage, for people using your API from
> C, but that's the way it works out.

 yehh

> Trying to play tricks where the string returned magically gets freed
> sometime in the future at an undefined time will definitely cause
> problems.

 yehhh... from dce/rpc's near-god-like status (in my mind, anyway) the
conventions are "caller responsible for freeing marshalled data" and
that's pure c, so i was kinda expecting people to advise this - but
what i was _really_ looking for was confirmation.

so - great!  thank you.

btw some kind person pointed out that i mis-read the python docs on
PyString_AsString so.... but... wait.... hang on, i think i still
might be in the clear....

ok - can i ask people a favour?  could you kindly review e.g.... this:
     http://lkcl.net/webkit/DerivedSources/GdomAttr.cpp

with a view to letting me know if it looks reasonable?  GStringConvert
takes care of the various types of [unicode] strings from webkit, to
convert them into a g_strdup()ed utf8 string.  if there wasn't already
a utf8 function in each of the webkit string types, i'd have to write
one - fortunately, it's already there so i don't.

just looking at it myself, i think where i use fromUTF8 i have a
memory leak, there, but key question: in the use of
g_value_set_string() - that "takes over" the memory passed in to it,
right?

l.


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]