[Interest] QList operator[]

Brad Pepers bpepers at me.com
Fri Jan 9 18:42:02 CET 2015


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.

I tested this on Windows using Qt 5.4.0 and VisualStudio 2010 and also on a Mac and get the same problem.  Could it be something about my CultureItem class?  Frankly I'm pretty baffled by this so any ideas?

Here is the CultureItem class definition in case that helps:

class CultureItem {
public:
    CultureItem();

    enum CultureType { PolyLine, Text, Contour };
    CultureType type;

    // shared attributes
    QString color;

    // PolyLine attributes
    int lineWidth;
    int fillPattern;
    bool smooth;
    bool closed;
    QString unknown1;
    QList<QPointF> points;

    // Text attributes
    float x;
    float y;
    float height;
    int justification;
    QString text;

    bool operator==(const CultureItem& rhs) const;
    bool operator!=(const CultureItem& rhs) const;
};

--
Brad

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20150109/79263daf/attachment.html>


More information about the Interest mailing list