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

Qt-jambi-interest Archive, November 2006
Garbage collection


Message 1 in thread

Hello,

one more jambi architecture question. If you are connecting an object A 
to another object B
signal, and object B is garbage collected, will object A get collected 
as well? Or do you
have to manually disconnect this object first to get rid of object A in 
memory?

Helge F.


Message 2 in thread

Helge Fredriksen wrote:

> one more jambi architecture question. If you are connecting an object 
> A to another object B
> signal, and object B is garbage collected, will object A get collected 
> as well? Or do you
> have to manually disconnect this object first to get rid of object A 
> in memory?
>

Hi, Helge.

Neither object A nor B will be garbage collected.

All top level QObjects (any object of a QObject subclass which has no 
parent) need to be explicitly disposed by calling the dispose() method. 
These objects will take care of disposing their children. Once a QObject 
has been disposed of, it is regarded as invalid, and any function call 
on it will cause an exception to occur.

-- Eskil


Message 3 in thread

Eskil A. Blomfeldt wrote:
> Helge Fredriksen wrote:
> 
>> one more jambi architecture question. If you are connecting an object 
>> A to another object B
>> signal, and object B is garbage collected, will object A get collected 
>> as well? Or do you
>> have to manually disconnect this object first to get rid of object A 
>> in memory?
>>
> 
> Hi, Helge.
> 
> Neither object A nor B will be garbage collected.
> 
> All top level QObjects (any object of a QObject subclass which has no 
> parent) need to be explicitly disposed by calling the dispose() method. 
> These objects will take care of disposing their children. Once a QObject 
> has been disposed of, it is regarded as invalid, and any function call 
> on it will cause an exception to occur.

When an object is disposed, all connections made or or from this object 
will be removed as well.


Message 4 in thread

Gunnar Sletta wrote:
> Eskil A. Blomfeldt wrote:
>> Helge Fredriksen wrote:
>>
>>> one more jambi architecture question. If you are connecting an 
>>> object A to another object B
>>> signal, and object B is garbage collected, will object A get 
>>> collected as well? Or do you
>>> have to manually disconnect this object first to get rid of object A 
>>> in memory?
>>>
>>
>> Hi, Helge.
>>
>> Neither object A nor B will be garbage collected.
>>
>> All top level QObjects (any object of a QObject subclass which has no 
>> parent) need to be explicitly disposed by calling the dispose() 
>> method. These objects will take care of disposing their children. 
>> Once a QObject has been disposed of, it is regarded as invalid, and 
>> any function call on it will cause an exception to occur.
>
> When an object is disposed, all connections made or or from this 
> object will be removed as well.
>
Hmm, this makes life a lot more awkward than what I'm used to. One of 
the major advantages of programming in java compared to C++ is that you 
don't need to think all that much about cleanup, since the garbage 
collector is your friend and takes care of wiping up after you. I think 
this really breaks with that way of thinking. Here I have to keep a 
tight track of all small QObjects that I make, else I will get a lot of 
memory leaks. And I probably will not be able to track these leaks in 
standard Java profilers like JProfiler either?

Helge F


Message 5 in thread

Helge Fredriksen wrote:

> Hmm, this makes life a lot more awkward than what I'm used to. One of 
> the major advantages of programming in java compared to C++ is that you 
> don't need to think all that much about cleanup, since the garbage 
> collector is your friend and takes care of wiping up after you. I think 
> this really breaks with that way of thinking. Here I have to keep a 
> tight track of all small QObjects that I make, else I will get a lot of 
> memory leaks. 

Hi Helge,

This is where the parent hierarchi comes into play ;-)

All QObjects have a parent that has ownership of the object, see for 
example QAbstractItemModel. You pass the parent to the constructor when 
the QObject is created, and when the parent is disposed all its children 
are collected too. When you build user interfaces using widgets and 
layouts, all of these QObjects end up in the parent/child hierarchy.

This means that to dispose of all the QObjects and QWidgets in a 
top-level dialog, you need only call dispose() on the top-level and it 
will take care of the rest. This is the same way that AWT / Swing works.

So in practice this is not a problem.

best regards,
Gunnar