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

Qt-interest Archive, December 2007
free up of memory based on parent object


Message 1 in thread

Hello,

I have the following function:

void Relationship::fillTable(int relNo){
        QTableWidgetItem *newItem = new QTableWidgetItem(counterpart);
        myPersona -> tableWidget -> setItem(relNo, 0, newItem);

}

This function is called multiple times, each time I create an object
*newItem. Qt documentation says that memory is freed up when the parent
object gets destroyed. For some constructors I can specify a parent object,
for QTableWidgetItem I can't. Does Qt automatically know which the parent
is? Or did I create a memory leak in my application? (I don't destroy the
object manually).

thanks!

--
 [ signature omitted ] 

Message 2 in thread

Hi Karl-Heinz, 

Look at the doco for QTableWidget.setItem. It says that the table widget
takes ownership of the item. 

Only QObject based objects use the parent parameter in constructor pattern. 

Not all objects in Qt are based on QObject for performance reasons. QObject
does a lot of different things, and all of these need to be cleaned up when
they are destroyed. 

Hope that helps, 

Tony Rietwyk.

> -----Original Message-----
> From: KH [mailto:karl-heinz-1950@xxxxxx] 
> Sent: Monday, 3 December 2007 00:25
> To: qt-interest@xxxxxxxxxxxxx
> Subject: free up of memory based on parent object
> 
> 
> Hello,
> 
> I have the following function:
> 
> void Relationship::fillTable(int relNo){
>         QTableWidgetItem *newItem = new QTableWidgetItem(counterpart);
>         myPersona -> tableWidget -> setItem(relNo, 0, newItem);
> 
> }
> 
> This function is called multiple times, each time I create an object
> *newItem. Qt documentation says that memory is freed up when 
> the parent
> object gets destroyed. For some constructors I can specify a 
> parent object,
> for QTableWidgetItem I can't. Does Qt automatically know 
> which the parent
> is? Or did I create a memory leak in my application? (I don't 
> destroy the
> object manually).
> 
> thanks!

--
 [ signature omitted ] 

Message 3 in thread

Hi,

so when the table widget is destroyed then these items are destroyed as
well? what would happen if the item is not assigned to table?

So I don't have memory leak. Did I understand you correctly?

Thanks!

Tony Rietwyk wrote:

> Hi Karl-Heinz,
> 
> Look at the doco for QTableWidget.setItem. It says that the table widget
> takes ownership of the item.
> 
> Only QObject based objects use the parent parameter in constructor
> pattern.
> 
> Not all objects in Qt are based on QObject for performance reasons.
> QObject does a lot of different things, and all of these need to be
> cleaned up when they are destroyed.
> 
> Hope that helps,
> 
> Tony Rietwyk.
> 
>> -----Original Message-----
>> From: KH [mailto:karl-heinz-1950@xxxxxx]
>> Sent: Monday, 3 December 2007 00:25
>> To: qt-interest@xxxxxxxxxxxxx
>> Subject: free up of memory based on parent object
>> 
>> 
>> Hello,
>> 
>> I have the following function:
>> 
>> void Relationship::fillTable(int relNo){
>>         QTableWidgetItem *newItem = new QTableWidgetItem(counterpart);
>>         myPersona -> tableWidget -> setItem(relNo, 0, newItem);
>> 
>> }
>> 
>> This function is called multiple times, each time I create an object
>> *newItem. Qt documentation says that memory is freed up when
>> the parent
>> object gets destroyed. For some constructors I can specify a
>> parent object,
>> for QTableWidgetItem I can't. Does Qt automatically know
>> which the parent
>> is? Or did I create a memory leak in my application? (I don't
>> destroy the
>> object manually).
>> 
>> thanks!
> 
> --
> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with
> "unsubscribe" in the subject or the body. List archive and information:
> http://lists.trolltech.com/qt-interest/

--
 [ signature omitted ] 

Message 4 in thread

Hi Karl-Heinz,

Yes, "taking ownership" means that the tablewidget will take care of
deleting it, you don't have to (and you shouldn't) delete them manually.

So, unless you create items that are not being "setItem"ed everything
should be fine.

Cheers,
Peter

karl-heinz wrote:
> so when the table widget is destroyed then these items are 
> destroyed as
> well? what would happen if the item is not assigned to table?
> 
> So I don't have memory leak. Did I understand you correctly?
> 
> Thanks!
> 
> Tony Rietwyk wrote:
> 
> > Hi Karl-Heinz,
> > 
> > Look at the doco for QTableWidget.setItem. It says that the 
> table widget
> > takes ownership of the item.

--
 [ signature omitted ] 

Message 5 in thread

Hi, 

"take ownership" means that the table will destroy the items. 

If you create a QTableWidgetItem, but don't call setItem, then you will have
to delete the item yourself. 

Tony

> -----Original Message-----
> From: KH [mailto:karl-heinz-1950@xxxxxx] 
> Sent: Monday, 3 December 2007 21:40
> To: qt-interest@xxxxxxxxxxxxx
> Subject: RE: free up of memory based on parent object
> 
> 
> Hi,
> 
> so when the table widget is destroyed then these items are 
> destroyed as
> well? what would happen if the item is not assigned to table?
> 
> So I don't have memory leak. Did I understand you correctly?
> 
> Thanks!
> 
> Tony Rietwyk wrote:
> 
> > Hi Karl-Heinz,
> > 
> > Look at the doco for QTableWidget.setItem. It says that the 
> table widget
> > takes ownership of the item.
> > 
> > Only QObject based objects use the parent parameter in constructor
> > pattern.
> > 
> > Not all objects in Qt are based on QObject for performance reasons.
> > QObject does a lot of different things, and all of these need to be
> > cleaned up when they are destroyed.
> > 
> > Hope that helps,
> > 
> > Tony Rietwyk.
> > 
> >> -----Original Message-----
> >> From: KH [mailto:karl-heinz-1950@xxxxxx]
> >> Sent: Monday, 3 December 2007 00:25
> >> To: qt-interest@xxxxxxxxxxxxx
> >> Subject: free up of memory based on parent object
> >> 
> >> 
> >> Hello,
> >> 
> >> I have the following function:
> >> 
> >> void Relationship::fillTable(int relNo){
> >>         QTableWidgetItem *newItem = new 
> QTableWidgetItem(counterpart);
> >>         myPersona -> tableWidget -> setItem(relNo, 0, newItem);
> >> 
> >> }
> >> 
> >> This function is called multiple times, each time I create 
> an object
> >> *newItem. Qt documentation says that memory is freed up when
> >> the parent
> >> object gets destroyed. For some constructors I can specify a
> >> parent object,
> >> for QTableWidgetItem I can't. Does Qt automatically know
> >> which the parent
> >> is? Or did I create a memory leak in my application? (I don't
> >> destroy the
> >> object manually).
> >> 
> >> thanks!
> > 
> > --
> > To unsubscribe - send a mail to 
> qt-interest-request@xxxxxxxxxxxxx with
> > "unsubscribe" in the subject or the body. List archive and 
> information:
> > http://lists.trolltech.com/qt-interest/
> 
> --
> To unsubscribe - send a mail to 
> qt-interest-request@xxxxxxxxxxxxx with "unsubscribe" in the 
> subject or the body.
> List archive and information: http://lists.trolltech.com/qt-interest/
> 

--
 [ signature omitted ]