Trolltech Home | Qt-jambi-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 1

Qt-jambi-interest Archive, November 2007
QObjects with no parents


Message 1 in thread

We're need to be able to use QObjects must dynamically be created and
cleaned up after their use is obsolete. If we don't set the parent to
these objects, and remove all references to them in the code, can we
then trust that the garbage collector will eventually remove all
resources (including native) on them?

Regards,
Helge F.


Message 2 in thread

Helge Fredriksen wrote:
> We're need to be able to use QObjects must dynamically be created and
> cleaned up after their use is obsolete. If we don't set the parent to
> these objects, and remove all references to them in the code, can we
> then trust that the garbage collector will eventually remove all
> resources (including native) on them?

Correct, if nobody is referencing a QObject, parent nor java reference, 
it will be collected by the gc, yes.

-
Gunnar


Message 3 in thread

Gunnar Sletta wrote:
> Helge Fredriksen wrote:
>> We're need to be able to use QObjects must dynamically be created and
>> cleaned up after their use is obsolete. If we don't set the parent to
>> these objects, and remove all references to them in the code, can we
>> then trust that the garbage collector will eventually remove all
>> resources (including native) on them?
> 
> Correct, if nobody is referencing a QObject, parent nor java reference, 
> it will be collected by the gc, yes.

There are cases in Qt that will reparent an object and thus give it a 
parent. The case I can think of is QLayout which will reparent any 
QWidgets to the widget the layout it laying out, so say you have:

QWidget parent = new QWidget();
QLayout layout = new QVBoxLayout(parent);

QPushButton button = new QPushButton();
layout.addWidget(button);

button will now be a child of parent.

-
Gunnar