My applications has a stability problem that I can't trace back to its root. I have a trayicon-menu from which I can create new notes. From within a note, I can delete that note by pressing a QToolButton.
The problem: On deleting, the application sometimes crashes. Most often when I compiled and freshly created 2 notes very fast and immediatly delete these notes. This process of deleting only touches the code below, but I can't find out, what I am doing wrong.
Sometimes it also crashes when deleting all notes, no matter how fast and the time or amount of notes existing.
I thought it could have something to do with trying to access m_noteList
by an index that it doesn't have. But I did check the value that is returned by getIndexByID
and it is OK...
traymenu.h:
...
public:
QList< QSharedPointer<Note> > m_noteList;
traymenu.cpp:
void Traymenu::newNote(){
QSharedPointer<Note> note(new Note(this));
m_noteList << note;
note.data()->setID(m_IDs); //the new note gets the current identifer m_IDs
setCurrentCounter(); //increment m_IDs by 1
void Traymenu::deleteNote(int ID){
int idx = getIndexByID(ID); /*compares all m_ID members of all
notes in m_noteList to match ID
=> returns index in list*/
m_noteList.removeAt(idx); //deleting the pointer actually removes the Note
}
int Traymenu::getIndexByID(int NoteID){
int idx=0;
while(NoteID != m_noteList[idx].data()->getID())
{
idx++;
}
return idx;
}
Note.h:
...
private:
Ui::Note *ui;
Traymenu * m_p_traymenu;
QWidget * m_p_parent;
int m_ID; //unique, random identifier to reopen and delete a specific note
...
Note.cpp:
Note::Note(Traymenu *trayMenuIn, QWidget *parent) :
ui(new Ui::Note){
ui->setupUi(this);
m_p_traymenu = trayMenuIn;
m_p_parent = parent;
setupNote(); //graphics, shadows, eventFilters, button position, resize...
show(); //not necessary for the first note, but for all following to be shown
}
void Note::s_delete(){ //Slot triggered by QToolButton
m_p_traymenu->deleteNote(m_ID);
}