LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-28-2008, 11:16 PM   #1
nonis
Member
 
Registered: Jan 2007
Posts: 95

Rep: Reputation: 15
deleting gtkwidget pointers


I'm writing some gtk GUI code lately, and most of it I've been learning from examples. I'm just curious as to why people don't delete their GtkWidget *s. Shouldn't they end up with a lot of memory wasted?

I guess I'm pretty rusty on C/C++ in all. Do you even need to delete pointers that are used throughout your whole program? Do they get deleted when the program exits anyway? Maybe this is why nobody deletes GtkWidget pointers.
 
Old 10-29-2008, 03:47 PM   #2
abolishtheun
Member
 
Registered: Mar 2008
Posts: 183

Rep: Reputation: 31
B/c GTK keeps track of its widgets via reference counting. You can decrement a reference via g_object_unref, but the actual garbage collection and freeing is taken care of by the gtk runtime when the count goes to zero.
 
Old 10-30-2008, 12:48 AM   #3
nonis
Member
 
Registered: Jan 2007
Posts: 95

Original Poster
Rep: Reputation: 15
That's what I figured. But then I have another question:
Do you lose memory if you don't delete a pointer by the end of the file.

Code:
int main()
{
int *pointer = new int(5);
return 0;
}
Should I delete pointer or does your program return all memory once it closes? Do you only have to worry about pointers during your program?
 
Old 10-30-2008, 04:30 AM   #4
abolishtheun
Member
 
Registered: Mar 2008
Posts: 183

Rep: Reputation: 31
all memory is recovered when a process ends... so technically you could get away with not freeing the memory, but its considered extremely bad practice; instead, make it a habit to free all resources you use (memory, files, sockets, devices, etc) before exiting. (you might find this tedious at first, but really, the only thing that separates a good C programmer from a bad one is discipline.) that is, unless you know the resource is garbage collected, like for gtk.
 
Old 10-30-2008, 12:06 PM   #5
nonis
Member
 
Registered: Jan 2007
Posts: 95

Original Poster
Rep: Reputation: 15
Okay, thanks! I was taught to always delete memory, but then after looking at GTK examples and tutorials I started questioning when to free memory. But now I've got it. [=
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
c pointers andystanfordjason Programming 3 04-22-2007 04:23 PM
Help with C pointers linuxlover1 Programming 13 07-05-2006 06:41 PM
How to draw the JPEG file into GtkWidget stny Programming 3 04-26-2006 08:14 PM
pointers hubabuba Programming 1 10-26-2005 07:47 PM
GtkWidget color prabhakar_kushwaha Programming 1 10-22-2005 01:06 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:18 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration