PDA

View Full Version : QList index out of range problem



MarkoSan
26th March 2008, 08:18
Hi to all!

I have following method with its code:
qint16 CApplicationSettings::searchSettingsForIndex(const QString& strHub, const QString& strKey)
{
if(m_SettingsValues.size()>0)
for (qint16 iIndex=0; iIndex=m_SettingsValues.size(); iIndex++)
if(m_SettingsValues.at(iIndex).strHub==strHub)
if(m_SettingsValues.at(iIndex).strKey==strKey)
return iIndex; // returns index of found record

return RECORD_NOT_FOUND; // not found
}Once called, the application crashes with following error:
warning: Lowest section in C:\WINDOWS\system32\xpsp2res.dll is .rsrc at 20001000
warning: QLayout: Cannot add null widget to QHBoxLayout/

Current language: auto; currently c++
warning: ASSERT failure in QList<T>::at: "index out of range", file ../../../../Qt/4.3.4/include/QtCore/../../src/corelib/tools/qlist.h, line 391It is obvious I try to read out of range record, but I simply do not get it why? This code seems perfect to me ... The app crashes in line 5 of method, in the first comparsion.

wysota
26th March 2008, 08:37
The loop is incorrect. The second argument of the for statement should probably say "iIndex<m_SettingsValues.size()". Now you are asigning an invalid index there and so the loop tries to read after the last element of the list.

MarkoSan
26th March 2008, 08:40
Ohhh, my good, what a retard mistake!!!! Thanks, wysota!!!