Wrong usage of QList::ConstIterator & QList::begin() ?

Thiago Macieira thiago at kde.org
Sat Oct 18 12:24:37 BST 2008


Albert Astals Cid wrote:
>A Dissabte 18 Octubre 2008, Christian Ehrlicher va escriure:
>> Hi,
>>
>> While trying to fix some bugs in KFile* on windows I saw some places
>> like this:
>>
>> for ( QStringList::ConstIterator it = m_dirList.begin();
>>        it != m_dirList.end() && !terminationRequested();
>>        ++it )
>>
>> Imo the programmer here expects that m_dirList.detach() isn't called
>> but this is wrong.
>>
>> m_dirList.begin() does a detach, returns a non-const iterator and then
>> the iterator is converted to a const iterator (see qlist.h:214 & 238).
>>
>> Therefore I suggest to add QT_STRICT_ITERATORS to the kdelibs flags.
>> Or does this maybe break BC?
>
>This is not wrong, just not what the developer "really wanted" but it
> works just fine, albeit a bit slower.

In this case.

In some cases, mixing const and non-const iterators can lead to crashes, 
due to the container detaching in-between and pointers moving around.

QT_STRICT_ITERATORS is good for development, but not for release code.

-- 
  Thiago Macieira  -  thiago (AT) macieira.info - thiago (AT) kde.org
    PGP/GPG: 0x6EF45358; fingerprint:
    E067 918B B660 DBD1 105C  966C 33F5 F005 6EF4 5358
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 189 bytes
Desc: This is a digitally signed message part.
URL: <http://mail.kde.org/pipermail/kde-core-devel/attachments/20081018/3db7c777/attachment.sig>


More information about the kde-core-devel mailing list