Re: g_strstrip question.



Geez this has gone on long enough.

first of all you cant just free any memory address.

char *str = g_strdup(...);

--> g_free(str);

is good

--> g_free(str[1]);

is worse than a memory leek (ie. the memory manager
never allocated memory for that address)

the same goes for the rest of the string.
(i.e if the trailing NULL charachter is no
longer the last allocated address; the _whole_
memory "chunk" will be freed)


second of all

strchug, strchop, strstrip
_all_ modify the string _directly_

NO memory has been allocated.

if you want a "chopped" duplicate you need to:

str2 = g_strchop(g_strdup(str1));

otherwise str1 is modified.


Cheers,
                        -Tristan


Martyn Russell wrote:

I think you'll find that 'b' is a new piece of memory and so is 'c',
therefore, you were right the first time in stating each variable would
have to be freed.  I am curious about one thing though, if g_strstrip is
a DEFINITION of a combination of g_strchug and g_strchop, that in it's
self should be a memory leak shouldn't it?

    gchar *a = NULL;
    gchar *b = NULL;
    gchar *c = NULL;

    a = g_strdup("   mystring   ");
    b = g_strchug(a);
    c = g_strchop(b);

    /* WHERE c is now the string u want */

    if(a != NULL) g_free(a);
    if(b != NULL) g_free(b);
    if(c != NULL) g_free(c);

Martyn

_______________________________________________
gtk-app-devel-list mailing list
gtk-app-devel-list gnome org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list



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