Qt-jambi-interest Archive, November 2007
Another garbage collection question
Message 1 in thread
In the same genre: If the answer to the previous question is YES, then,
is it any point in setting the parent to a QObject at all? Isn't this a
pattern beeing inherited from the C++ world where automatic deletion of
children is wanted when a parent is deleted. This shouldn't really be
necessary the Java setting where the garbage collection should deal with
such problems.
Best regards,
Helge Fredriksen
Message 2 in thread
Helge Fredriksen wrote:
> In the same genre: If the answer to the previous question is YES, then,
> is it any point in setting the parent to a QObject at all? Isn't this a
> pattern beeing inherited from the C++ world where automatic deletion of
> children is wanted when a parent is deleted. This shouldn't really be
> necessary the Java setting where the garbage collection should deal with
> such problems.
Hi Helge,
Very good questions!
Yes, the parent is inherited from C++, but it is the equivalent of any
tree node holding a list of references to its children, which is how you
would design any other Java tree structure, so I would definitly say its
an expected feature in Java. Consider the:
QDialog createDialog(DialogType type);
That instantiates all the contents of a dialog and returns the toplevel
dialog only. If QDialog didn't keep a list of all its children, they
could be collected by the gc at any time, and this the dialog would
first show up, then slowly start to fall apart as the GC collected
pieces of it. This is not really the behavior you expect. What you
expect is since you keep a reference to QDialog and QDialog contains
elements, these elements should be "referenced" as long as QDialog remains.
For QObjects, I agree that the child/parent relationship is less
meaningful, but there are also extremely few cases where QObjects are
used outside the widget hierarchy.
best regards,
Gunnar