LinuxQuestions.org
Review your favorite Linux distribution.
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 03-28-2006, 06:58 PM   #1
numen
LQ Newbie
 
Registered: Nov 2005
Location: China
Posts: 15

Rep: Reputation: 0
qt has auto memory management


i am a qt learner. i download the sdk of qt from trolltech company.
i read the examples of the sdk, it's unbelivevable that all the resourse alloced by operator new are never freed.
why? qt has apply a garbage collector ? resources alloced by operator new should nerver been freed by myself?

thanks.
 
Old 03-28-2006, 08:23 PM   #2
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 42
In general I think a parent widget frees its children in its destructor. So if you create a button with 'new' and then never use it anywhere, you would have to free it, but in general you don't have to free anything because all your widgets get parented by something else.

IIRC, something like QApplication is at the top of this heirarchy, and it either lasts the duration of your program or you make it on the stack.

It's been a while and I haven't done a lot of qt so this is a vagueish answer.
 
Old 03-29-2006, 05:01 AM   #3
numen
LQ Newbie
 
Registered: Nov 2005
Location: China
Posts: 15

Original Poster
Rep: Reputation: 0
thanks , maybe i need further study the memory management of qt.
it's very important for programming principle. i would rather think that qt
doesn't apply the auto garbage collector for effiency reason.
 
Old 03-29-2006, 07:49 AM   #4
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
If I remember correctly anything that inherits QObject will have the allocated memory managed for you.
 
Old 03-29-2006, 11:35 AM   #5
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 42
It's not a garbage collector; a parent widget doesn't need to do any searching or trickiness to figure out what to delete. It just deletes its children. (I wouldn't be suprised to see some refcounting somewehere or other, also)

This is nothing like the java garbage collector, for example.
 
Old 03-30-2006, 05:04 AM   #6
numen
LQ Newbie
 
Registered: Nov 2005
Location: China
Posts: 15

Original Poster
Rep: Reputation: 0
thank you !
do you mean that?:
anything derived from QObject has no need memory management yourself!
for your own class ,you have to do the memory management?
 
Old 03-30-2006, 08:37 AM   #7
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 42
if your class is some sort of widget that gets parented by something in Qt, you know that your destructor will get called for you. Of course it's still up to you to free any pointers you're carrying in your object. For non-Qt stuff, of course it's all up to you
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
memory management navaladi Mandriva 2 02-18-2005 06:47 AM
Memory management Mojojo Linux - Hardware 11 08-31-2003 10:29 AM
memory management exigent Linux - General 1 08-17-2003 08:28 PM
Memory Management hhegab Linux - General 3 08-07-2002 10:20 PM
Memory Management mrsolo Linux - General 7 06-26-2002 12:55 AM


All times are GMT -5. The time now is 07:12 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration