GTK+ Forums Forum Index GTK+ Forums
Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

GTK cleanup

 
Post new topic   Reply to topic    GTK+ Forums Forum Index -> General GTK+ Discussion
Author Message
Peter
GTK+ Geek


Joined: 10 May 2008
Posts: 54

PostPosted: Sun May 18, 2008 1:17 am    Post subject: GTK cleanup Reply with quote

How important to clean up memory/objects allocated?

What is being automatically released what the program quits and what is not?

If I creatre a non-static object within a function is the memory released when I exit
the function?

For instance:
Code: (C)
1
2
3
4
5
6
7
8
9

 MyFinction ()
{
     GtkObject *someObject;
     someObject = create_new_object();
     ...
     return;
}

Will in this example the memory used by someObject released after return or I should use free_objects(omeObject) [these are ficticious functions...]?
Peter
Back to top
Micah Carrick
Never Seen the Sunlight


Joined: 21 Sep 2005
Posts: 505
Location: Portland, OR USA

PostPosted: Mon May 19, 2008 3:42 am    Post subject: Reply with quote

It's best to assume that it's very important to cleanup all your objects and allocated memory when working in C or other languages which do not use garbage collection.

While it may be true that the operating system will be smart enough to clean up the memory you application leaked after it has been terminated, you can't rely on that as the programmer.

Get in the habit and it'll become second nature.

However, that applies to objects or memory that has been allocated. As far as GTK+ goes, if you use g_new, malloc, g_slice_new, etc. then you need to free that memory.

With GTK, most objects which need to be freed by the developer include gtk_foobar_free() functions so that's usually how you can tell. If it doesn't, then you don't need to worry about it, GTK is probably going to take care of it for you.

There are also situations where you need to "unref" an object so that GTK knows it's time to free it... this is usually true with pixbufs and Glade XML objects and the like.

For all intents and purposes, you can read what the documetation says for the return value of a function. If it returns an "newly allocated" anything, you will probably have to free that explicitly when you are done using it. These will typically have a _free() function. If a function returns a "newly allocated" gchar*, you will free that using g_free().

In the case of your example object... it depends on the object. Some of the more common objects you'll have to free are GStrings, GErrors, GList, and some of those basic GLib data types. Things like GtkTextView and the like you typically don't need to free because even though you are no longer referencing them (when you leave that function) they are still being used to comprise your GUI. When the window to which they are children is destroyed, then they will be destroyed as well and their memory freed.
Back to top
Peter
GTK+ Geek


Joined: 10 May 2008
Posts: 54

PostPosted: Mon May 19, 2008 2:44 pm    Post subject: Reply with quote

Thank you Micah.

Very useful points!

I have a habit when I do Windows programming: whenever I type
CreateObject(), immediately I type DeleteObject() on the next line.
Then I insert the stuff between. This way I don't forget to dispose the object.

Peter


Micah Carrick wrote:
It's best to assume that it's very important to cleanup all your objects and allocated memory when working in C or other languages which do not use garbage collection.

While it may be true that the operating system will be smart enough to clean up the memory you application leaked after it has been terminated, you can't rely on that as the programmer.

Get in the habit and it'll become second nature.

However, that applies to objects or memory that has been allocated. As far as GTK+ goes, if you use g_new, malloc, g_slice_new, etc. then you need to free that memory.

With GTK, most objects which need to be freed by the developer include gtk_foobar_free() functions so that's usually how you can tell. If it doesn't, then you don't need to worry about it, GTK is probably going to take care of it for you.

There are also situations where you need to "unref" an object so that GTK knows it's time to free it... this is usually true with pixbufs and Glade XML objects and the like.

For all intents and purposes, you can read what the documetation says for the return value of a function. If it returns an "newly allocated" anything, you will probably have to free that explicitly when you are done using it. These will typically have a _free() function. If a function returns a "newly allocated" gchar*, you will free that using g_free().

In the case of your example object... it depends on the object. Some of the more common objects you'll have to free are GStrings, GErrors, GList, and some of those basic GLib data types. Things like GtkTextView and the like you typically don't need to free because even though you are no longer referencing them (when you leave that function) they are still being used to comprise your GUI. When the window to which they are children is destroyed, then they will be destroyed as well and their memory freed.
Back to top
Display posts from previous:   
Post new topic   Reply to topic    GTK+ Forums Forum Index -> General GTK+ Discussion All times are GMT
Page 1 of 1

 


Powered by phpBB © 2001, 2005 phpBB Group
CodeBB 1.0 Beta 2
Protected by Anti-Spam ACP