[Qt-interest] Qt memory management.

Andre Somers andre at familiesomers.nl
Wed Aug 18 12:49:38 CEST 2010


  Op 18-8-2010 12:29, Ramesh schreef:
> Hi arnold,
> Thanks for the reply...
>
> With copy constructor I can copy one widget to another widget?..
Copying widgets (or objects) should not even compile, AFAIK. Their copy 
and assignment constructors are private. Of course, you can pass around 
pointers to them freely.
> And what about the reference count means?. I saw the QString class
> destructor, there before deleting the object they check for reference count.
> But QString is not a QObject right.
>
> So it means, assignment of one Qstring to another does copy or not?
Copying a QString makes a shallow copy. That means, that it will point 
to the same data structure that contains the actual data, and that the 
refcount of that internal structure will be increased. So, there is a 
copy going on, but a very cheap one (just a pointer). As soon as an 
operation starts that would alter data on an object with a refcount > 1, 
the copies will detach. That means that the copy of the actual data will 
take place at that time, which is way more data than just that one pointer.

So, in your example with QString:

QString a = "foo"; // "QStringPrivate" has a refcount of 1
a = "foo2"; //no copy, still a refcount of 1
QString b = a; // Shallow copy of QStringPrivate, that now has a 
refcount of 2
QString b = "bar"; // detach! A deep copy will occur, so we have two 
individual QStringPrivate's each with a refcount of 1
b = "bar2"; //no copy needed anymore.

André

>
>
> -----Original Message-----
> From: qt-interest-bounces at trolltech.com
> [mailto:qt-interest-bounces at trolltech.com] On Behalf Of Arnold Krille
> Sent: Wednesday, August 18, 2010 3:40 PM
> To: qt-interest at trolltech.com
> Subject: Re: [Qt-interest] Qt memory management.
>
> Hi,
>
> On Wednesday 18 August 2010 11:15:19 Ramesh wrote:
>> Qt widgets uses shared memory so  we can't copy a widget to another
>> widget right?
> No, QWidgets (and all QObjects) are organized in a tree. And copying within
> a tree or from one tree to another needs copy-constructors and -operators
> with two arguments (original and new parent).
>
>> But in this case
>> p2 = p1;
>> both are Qpixmap object, and Qpixmap is not inherited from Qobject so
>> assigning a pixamp to another leads to copy operation right?.
> Yes.
>
> Have fun,
>
> Arnold
>
>
> -----------------------------------------------
> Robosoft Technologies - Come home to Technology
>
> Disclaimer: This email may contain confidential material. If you were not an intended recipient, please notify the sender and delete all copies. Emails to and from our network may be logged and monitored. This email and its attachments are scanned for virus by our scanners and are believed to be safe. However, no warranty is given that this email is free of malicious content or virus.
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest
>




More information about the Qt-interest-old mailing list