<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Topics tagged with listiterator]]></title><description><![CDATA[A list of topics that have been tagged with listiterator]]></description><link>https://forum.qt.io/tags/listiterator</link><generator>RSS for Node</generator><lastBuildDate>Fri, 25 Sep 2026 02:26:43 GMT</lastBuildDate><atom:link href="https://forum.qt.io/tags/listiterator.rss" rel="self" type="application/rss+xml"/><pubDate>Invalid Date</pubDate><ttl>60</ttl><item><title><![CDATA[QListIterator memory corruption]]></title><description><![CDATA[<p dir="auto">Hello! I have a little trouble understanding how QListIterator/QMutableListIterator work.<br />
The following example is based on a real world application, I just isolated the problematic code so it's easier to understand.<br />
If you want to try it, just create a new console application in Qt Creator and replace main.cpp with this code. I used the precompiled Qt 5.4.2 SDK with the MinGW compiler.</p>
<pre><code>#include &lt;QCoreApplication&gt;
#include &lt;QDebug&gt;

struct A {
    A(): a(1), b(1), c(3) {}
    int a, b, c;
};

struct B {
    QList&lt;A&gt; list;
    void inner() {
        QMutableListIterator&lt;A&gt; it(list);
        while (it.hasNext()) {
            it.next().b++;
        }
    }
};

static B b;

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);


    for (int i = 0; i &lt; 10; i++) {
        A a;
        b.list.append(a);
    }

    A* first = &amp;b.list[0];

    {
        QListIterator&lt;A&gt; it(b.list);
        while (it.hasNext()) {
            const A&amp; elem = it.next();
            qDebug() &lt;&lt; elem.a &lt;&lt; elem.b &lt;&lt; elem.c;
        }
        b.inner();
        qDebug() &lt;&lt; first-&gt;a &lt;&lt; first-&gt;b &lt;&lt; first-&gt;c;
    }

    qDebug() &lt;&lt; first-&gt;a &lt;&lt; first-&gt;b &lt;&lt; first-&gt;c;

    return a.exec();
}
</code></pre>
<p dir="auto">Here is the output:</p>
<pre><code>1 1 3
1 1 3
1 1 3
1 1 3
1 1 3
1 1 3
1 1 3
1 1 3
1 1 3
1 1 3
1 1 3
-17891602 -17891602 -17891602
</code></pre>
<p dir="auto">The last two lines puzzle me. I was expecting them both to say "1 2 3", but instead ~QListIterator destructed the list, and I'm reading deallocated memory.<br />
Is this how it is supposed to work, and if yes, why?</p>
]]></description><link>https://forum.qt.io/topic/55548/qlistiterator-memory-corruption</link><guid isPermaLink="true">https://forum.qt.io/topic/55548/qlistiterator-memory-corruption</guid><dc:creator><![CDATA[Simon Parzer]]></dc:creator><pubDate>Invalid Date</pubDate></item></channel></rss>