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

Qt-jambi-interest Archive, February 2008
Jambi Memory Usage


Message 1 in thread

Hi all,

I am trying to understand how Jambi uses memory and
when objects will be garbage collected. For example:

QDialog a = new MyDialog();
a.exec();

Somewhere inside MyDialog there is a button that, when
clicked, calls:

this.accept();

After calling accept or reject, is the dialog ready to
be garbage collected ? When will this happen ? How can
I make sure that the dialog will be collected after it
is closed ?

The same question about widgets inside other dialogs
(widgets). Will they be garbage collected and the
memory released after the parent widget is closed ?
How can I make sure this will happens and no dangling
object references remain ?

I know for sure that show() and hide() do not destroy
the widget, but what about accept and reject in case
of the QDialog ?

The same kind of question for what I saw one of our
developers doing, and I don't know if it is correct.
They have a QToolBox and, each time the QDialog is
shown (on showEvent), they clear the QToolBox with an
iterrative removeItem and then recreate it with
addItem (this needs to be done as the tool box changes
depending on external parameters). By doing this, does
the removeItem make the object (QItemWidget) ready for
garbage collection or there is an additional step that
needs to be done in order to be sure that it won't
create a memory leak ?

Sorry for the long email, but I really need to know
how to avoid memory problems and how to be sure that
an object will be garbage collected...

Thanks,
Danny 


      ____________________________________________________________________________________
Be a better friend, newshound, and 
know-it-all with Yahoo! Mobile.  Try it now.  http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ 


Message 2 in thread

Danny Sporea wrote:
> I am trying to understand how Jambi uses memory and
> when objects will be garbage collected. For example:
> 
> QDialog a = new MyDialog();
> a.exec();
> 
> Somewhere inside MyDialog there is a button that, when
> clicked, calls:
> 
> this.accept();
> 
> After calling accept or reject, is the dialog ready to
> be garbage collected ? When will this happen ? How can
> I make sure that the dialog will be collected after it
> is closed ?

Hi Danny,

Since this is Java the short answer is, you don't have control and GC 
will happen when it needs to and may or may not happen when the dialog 
is ready for deletion. It works the same as any other Java library. 
There is of course things like System.gc() that can be used to urge 
memory to be collected, but you cannot rely on this to happen. However...

Qt Jambi does allow some of its C++ magic to slip into Java. When you 
are done with a dialog you can simply delete it. This is done using the 
dispose method. I strongly urge you to use this with caution though as 
there are a number of cases where explict deletion may have nasty side 
effects, specially never delete an object while there are _any_ other 
references to it or while you are in one of its virtual functions, like 
an event handler. For the case of:

QDialog d = new MyDialog();
d.exec();
readDataFromDialog(d);
d.dispose();

is completly safe, and can simplify garbage collection a bit.

> The same question about widgets inside other dialogs
> (widgets). Will they be garbage collected and the
> memory released after the parent widget is closed ?

If a parent of a QWidget is deleted, then the C++ part of the object 
will be collected also. If you try to use a QPushButton child of 
MyDialog above after the dispose call you will get an exception, as the 
object is in fact deleted.

> How can I make sure this will happens and no dangling
> object references remain ?

Qt Jambi will normally handle this. You can speed up the process by 
calling dispose on an object, but normally you wouldn't need to. When 
there are no references to an object it will be available for GC.

> I know for sure that show() and hide() do not destroy
> the widget, but what about accept and reject in case
> of the QDialog ?

accept() or reject() does not automatically delete the widget. This 
would mean that you could never read the content of an accepted dialog 
which would defy the point of popping up the dialog in the first place ;-)

> The same kind of question for what I saw one of our
> developers doing, and I don't know if it is correct.
> They have a QToolBox and, each time the QDialog is
> shown (on showEvent), they clear the QToolBox with an
> iterrative removeItem and then recreate it with
> addItem (this needs to be done as the tool box changes
> depending on external parameters). By doing this, does
> the removeItem make the object (QItemWidget) ready for
> garbage collection or there is an additional step that
> needs to be done in order to be sure that it won't
> create a memory leak ?

If neither you nor the toolbox hold a reference to the widget it will be 
collected (at some point), same as any other Java library.

> Sorry for the long email, but I really need to know
> how to avoid memory problems and how to be sure that
> an object will be garbage collected...

Qt Jambi does provide some features that simplify garbage collection and 
makes it possible to tweak memory usage a bit more than normal Java 
toolkits, but these are primarily optional features.

As I said in the beginning, we're following normal Java rules, so you 
don't create memory through the use patterns above.

-
Gunnar