[Interest] QList and erase problem...

Igor Mironchik igor.mironchik at gmail.com
Mon Sep 14 17:29:55 CEST 2015


Look in while statement....

9/14/2015 6:28 PM, Croitor Alexandru пишет:
> The documentation for QList::erase says:
>
> -- Removes the item associated with the iterator /pos/ from the list, 
> and returns an iterator to the next item in the list (which may be end 
> <file:///Users/placinta/Library/Application%20Support/Dash/DocSets/Qt_5/Qt.docset/Contents/Resources/Documents/doc.qt.io/qt-5/qlist.html#end>()).
>
> Documentation for iterator::operator++() says:
>
> Calling this function on QList::end 
> <file:///Users/placinta/Library/Application%20Support/Dash/DocSets/Qt_5/Qt.docset/Contents/Resources/Documents/doc.qt.io/qt-5/qlist.html#end>() 
> leads to undefined results.
>
> You have no check in your code to see if the iterator return by erase 
> is QList::end(), thus calling operator++ on it might crash your 
> application.
>
>
>
> On Mon, Sep 14, 2015 at 6:13 PM, Igor Mironchik 
> <igor.mironchik at gmail.com <mailto:igor.mironchik at gmail.com>> wrote:
>
>     Hi,
>
>     it seems that problem not in the iterating and deletion from
>     cantainer...
>
>     I wrote simple test app and everything is ok.
>
>     But something here crashes my app and only in release mode, so I can't
>     understand what is the problem...
>
>     And ideas?
>
>     9/14/2015 6:06 PM, André Somers пишет:
>     > Op 14-9-2015 om 16:57 schreef Igor Mironchik:
>     >> Hi,
>     >>
>     >> I ran into this problem...
>     >>
>     >> void
>     >> ImageFilesStoragePrivate::removeAllImages(
>     >>        QList< ImageRecord > & images,
>     >>        ImageFilesStorage::ImageType type )
>     >> {
>     >>        QList< ImageRecord >::Iterator it = images.begin();
>     >>        QList< ImageRecord >::Iterator last = images.end();
>     >>
>     >>        while( it != last )
>     >>        {
>     >>            if( (*it).m_type == type )
>     >>            {
>     >>                LOG( DebugLogLevel, "Before real deletion" )
>     >>                removeFromDatabase( it );
>     >>                LOG( DebugLogLevel, "After real deletion" )
>     >>
>     >> //            it = images.erase( it ); // LOOK AT THIS LINE !!!
>     If I
>     >> uncomment it and comment next line - crash...
>     >>                ++it;
>     >>
>     >>                LOG( DebugLogLevel, QString( "it == last : %1"
>     ).arg( ( it
>     >> == last ? "true" : "false" ) ) )
>     >>            }
>     >>            else
>     >>                ++it;
>     >>        }
>     >> }
>     >>
>     >> What is wrong?
>     > A lot is wrong.
>     >
>     > First, you are not using standard algorithms to do standard
>     things. I'd
>     > say std::remove_if with std::erase would be a better way to
>     solve the issue.
>     >
>     > Then in general, if you delete from the container, iterators are
>     going
>     > to be invalidated. Iterate backwards if you are going to delete
>     items
>     > from the container.
>     >
>     > Last: you probably don't want to use QList but QVector.
>     >
>     > André
>     >
>     >
>     > _______________________________________________
>     > Interest mailing list
>     > Interest at qt-project.org <mailto:Interest at qt-project.org>
>     > http://lists.qt-project.org/mailman/listinfo/interest
>
>     _______________________________________________
>     Interest mailing list
>     Interest at qt-project.org <mailto:Interest at qt-project.org>
>     http://lists.qt-project.org/mailman/listinfo/interest
>
>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20150914/516354d1/attachment.html>


More information about the Interest mailing list