Originally Posted by
ChrisW67
If there are "numb" elements in the array you have space allocated for "numb" values then the last valid index is "numb-1". Trying to access array[numb] is a recipe for out-of-bounds memory violations.
Hello Chris,
well this is not actually in my test code, i think I copied wrong. Here is the code that is not working:
void AlarmManagment::readExistingAlarms(Alarm *alarms[], int numb)
{
/**** Copy current data ****/
numberofalarms2 = numb;
for (int aaa = 0; aaa < numberofalarms2; aaa++)
{
v_Alarm2[aaa] = alarms[aaa];
qDebug() << "Copied: " << v_Alarm2[aaa]->getDia();
qDebug() << "From: " << alarms[aaa]->getDia();
}
/**** Add to the list all alarms ****/
for (int zzz = 0; zzz < numberofalarms2; zzz++)
{
if (!vector_String1.contains(v_Alarm2[zzz]->getDia()))
{
vector_String1.push_back(v_Alarm2[zzz]->getDia());
qDebug() << "Antes do addRoot 2";
addRoot(v_Alarm2[zzz]->getDia(), v_Alarm2[zzz]->getDiadaSemana(), v_Alarm2[zzz]->getHora(),
v_Alarm2[zzz]->getSmallDescription());
addToYearandMonthCB(v_Alarm2[zzz]->getDiaOnlyYear(),v_Alarm2[zzz]->getDiaOnlyMonth());
}
else
{
listaDeQTreeWidgetItems = ui->Listofalarms->findItems(v_Alarm2[zzz]->getDia(),Qt::MatchExactly,0);
addChild(listaDeQTreeWidgetItems.front(), v_Alarm2[zzz]->getDiadaSemana() ,v_Alarm2[zzz]->getHora(),
v_Alarm2[zzz]->getSmallDescription());
}
}
}
void AlarmManagment::readExistingAlarms(Alarm *alarms[], int numb)
{
/**** Copy current data ****/
numberofalarms2 = numb;
for (int aaa = 0; aaa < numberofalarms2; aaa++)
{
v_Alarm2[aaa] = alarms[aaa];
qDebug() << "Copied: " << v_Alarm2[aaa]->getDia();
qDebug() << "From: " << alarms[aaa]->getDia();
}
/**** Add to the list all alarms ****/
for (int zzz = 0; zzz < numberofalarms2; zzz++)
{
if (!vector_String1.contains(v_Alarm2[zzz]->getDia()))
{
vector_String1.push_back(v_Alarm2[zzz]->getDia());
qDebug() << "Antes do addRoot 2";
addRoot(v_Alarm2[zzz]->getDia(), v_Alarm2[zzz]->getDiadaSemana(), v_Alarm2[zzz]->getHora(),
v_Alarm2[zzz]->getSmallDescription());
addToYearandMonthCB(v_Alarm2[zzz]->getDiaOnlyYear(),v_Alarm2[zzz]->getDiaOnlyMonth());
}
else
{
listaDeQTreeWidgetItems = ui->Listofalarms->findItems(v_Alarm2[zzz]->getDia(),Qt::MatchExactly,0);
addChild(listaDeQTreeWidgetItems.front(), v_Alarm2[zzz]->getDiadaSemana() ,v_Alarm2[zzz]->getHora(),
v_Alarm2[zzz]->getSmallDescription());
}
}
}
To copy to clipboard, switch view to plain text mode
When I run the Debugger, it points error not in the process of copy (which my test show that runs fine), but in the addRoot() function. The addRoot() function is:
{
itm->setText(0,data);
itm2->setText(0,semana);
itm2->setText(1,hora);
itm2->setText(2,smadesc);
itm->addChild(itm2);
}
void AlarmManagment::addRoot(QString data, QString semana, QString hora, QString smadesc)
{
QTreeWidgetItem *itm = new QTreeWidgetItem(ui->Listofalarms);
itm->setText(0,data);
QTreeWidgetItem *itm2 = new QTreeWidgetItem();
itm2->setText(0,semana);
itm2->setText(1,hora);
itm2->setText(2,smadesc);
itm->addChild(itm2);
}
To copy to clipboard, switch view to plain text mode
and the error is pointed to the first line of this function. Very interestingly, if I commet all the second for(; where the addRoot() function is, no problems occurs, so it is like the way I copy the pointers is not problematic, but makes another part of the code to be so.
Where is space to hold the array of pointers, called vector2, allocated? I suspect you are never doing that.
Hmm what exactly do you mean by that?
Thanks,
Momergil
Bookmarks