Re: freeing gslist data ?



On Sun, Jun 22, 2003 at 06:25:04PM -0500, James M. Cape wrote:
On Sun, 2003-06-22 at 17:04, Mohammed Sameer wrote:
Hi,
suppose that i have a GSList *foo and a gchar *bar
and i've used something like this:
foo = g_slist_append(foo, g_strdup(bar));
now when i free the list, Do i have to manually iterate over the list 
and free the ->data ?
i've browsed the code but i didn't understand well the gallocator things

Yes, you do need to free the data yourself. For a list of strings,
g_slist_foreach (list, (GForeachFunc) g_free, NULL); should do it.

The GAllocator stuff is just the way GLib handles lists & such. It
allocates a bunch of them, and when you do g_(s)list_free, it puts them
back in the pool for other code to use (so it doesn't actually free the
list, it just marks the list items for re-use).


Thanks Ali and James for the tip!

g_slist_foreach (list, (GFunc) g_free, NULL); is fine, I think i'll be 
missing mem. leaks ;)

-- 
----------------
-- Katoob Main Developer
Linux registered user # 224950
ICQ # 58475622
FIRST make it run, THEN make it run fast "Brian Kernighan".
--
Don't send me any attachment in Micro$oft (.DOC, .PPT) format please
Read http://www.fsf.org/philosophy/no-word-attachments.html
Preferable attachments: .PDF, .HTML, .TXT
Thanx for adding this text to Your signature

Attachment: pgps3sk7oJp_K.pgp
Description: PGP signature



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