Re: Behaviour of getters wrt dup/ref
- From: David Nečas (Yeti) <yeti physics muni cz>
- To: gtk-devel-list gnome org
- Subject: Re: Behaviour of getters wrt dup/ref
- Date: Sun, 16 Sep 2007 09:03:25 +0200
On Sat, Sep 15, 2007 at 11:08:38PM -0400, Behdad Esfahbod wrote:
> On Fri, 2007-09-14 at 10:35 -0400, Alexander Larsson wrote:
> >
> > char * g_data_input_stream_get_line (GDataInputStream *data_stream,
> > gsize *length,
> > GCancellable *cancellable,
> > GError **error);
> >
> > This actually reads new data from the stream, so it has to dup. One
> > could imagine a similar call that returns some form of object instead
> > of a string.
>
> I think it's pretty common in glib and pango at least to return
> g_strdup'ed strings. The no-ref-count rule is mostly for objects that
> have a literal ref/unref pair.
>
> Other than that, for functions that return read data from the stream,
> some people may have reasons to want to avoid malloc/free'ing on each
> line. One way to work around that is to have the function take a
> GString, so you can reuse the buffer from the previous line. I know
> most people are not a big fan of that idea though.
The right interface for this type of functions have been
already invented: that of glibc's getline. It can allocate
new buffers, it can reuse existing buffers resizing them if
necessary -- and it can be even used with GStrings [if they
use the same memory allocator] although that's a bit dirty.
Yeti
--
http://gwyddion.net/
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]