[Interest] QList operator[]
André Pönitz
apoenitz at t-online.de
Fri Jan 9 20:40:03 CET 2015
On Fri, Jan 09, 2015 at 05:42:02PM +0000, Brad Pepers wrote:
> I added a couple lines of code to an app and suddenly it started chewing
> through memory at an unbelievable rate. I watched it get up to 12 GB at
> which point it hit my virtual memory limit in Windows and Windows
> promptly blue-screened and died (thank-you Microsoft!). The changes I
> made are the two commented lines in this code:
>
> for (int i = 0; i < culture.items.size(); ++i) { const
> CultureItem& it = culture.items[i]; // added this qDebug() <<
> it.points.size(); // and this CultureGraphicsItem* item = new
> CultureGraphicsItem(group); item->setCulture(culture, i,
> _coordSys); }
>
> The culture class has an items member which is a QList<CultureItem> and a
> CultureItem has a QList<QPointF> of points so all I wanted was to print
> out the number of points each culture item has. I was stumped by this
> until I found that if I just replace the culture.items[i] with
> culture.items.at(i), the memory problem goes away. So it seems like even
> though I'm getting a const reference using operator [] on the QList, it's
> actually making a deep copy of the data as if I was using the non-const
> version.
[] on a non-const list detaches, independently of what you do with the
result. If you only need a 'const' use of the result on a non-const list,
you should use .at(...).
Andre'
More information about the Interest
mailing list