[10670] in The GTK GIMP ToolKit mailing list archive

home help back first fref pref prev next nref lref last post

[gtk-list] Re: potential problems with malloc/free vs. C++ new/delete ???

daemon@ATHENA.MIT.EDU (Dave Reed)
Thu Dec 3 20:21:54 1998

Date: Thu, 3 Dec 1998 20:25:14 -0500
From: Dave Reed <dreed@capital.edu>
To: gtk-list@redhat.com
In-Reply-To: <Pine.GSO.3.95.981203182229.22549T-100000@harper.uchicago.edu>
	(message from Havoc Pennington on Thu, 3 Dec 1998 18:25:19 -0600 (CST))
Resent-From: gtk-list@redhat.com
Reply-To: gtk-list@redhat.com

> From: Havoc Pennington <rhpennin@midway.uchicago.edu>
> On Thu, 3 Dec 1998, Dave Reed wrote:
> > 
> > So do I have a memory leak then?  What is the proper way around this?
> > Do I have to remember to delete all the strings myself after calling
> > the gtk_clist_clear() function.  Or can I delete them after adding
> > them to the clist?  I thought I tried using the same string with
> > different values each time I added them to the clist and I ended up
> > with all the entries being the same as the last one I had set the
> > string to.
> > 
> > e.g,
> > 
> > sprintf(str, "a");
> > // add to clist
> > sprintf(str, "b");
> > // add to clist
> > 
> > and both entries would be b.
> > 
> 
> Should work, maybe something else was wrong. I usually end up doing this:

Yes, you're correct, it does work - here is the example I wasn't
remembering:

The g_list clearly must just keep a pointer to the data so I had to
have a separate string for each one.

  char *listStr;

  while (nextCatHead(heading)) {
    heading->initIterator();
    while (heading->next(category)) {
      listStr = (char *) g_malloc(sizeof(char) * 100);
      sprintf(listStr, "%s %s", category->id().c_str(),
	      category->name().c_str());
      catItems = g_list_append(catItems, listStr);
    }
  }
  gtk_combo_set_popdown_strings(GTK_COMBO(categoryCombo_), catItems);

Should I be freeing anything here after it gets added to the combo?
What part of the documentation should I be looking in to find the
answers to these kind of memory questions?

Thanks again - you've been a big help.
Dave
dreed@capital.edu

-- 
To unsubscribe: mail -s unsubscribe gtk-list-request@redhat.com < /dev/null


home help back first fref pref prev next nref lref last post