我的應用程序有一個穩定性問題,我無法追溯到它的根目錄。我有一個托盤圖標菜單,可以從中創建新筆記。從筆記中,我可以通過按QToolButton刪除該筆記。當對象的指針被刪除時(刪除對象),應用程序會隨機崩潰
問題: 在刪除時,應用程序有時會崩潰。大多數情況下,當我編譯並且非常快速地創建2個筆記並立即刪除這些筆記時。這個刪除過程只涉及下面的代碼,但我找不到,我做錯了什麼。
有時它也會在刪除所有筆記時崩潰,無論筆記存在多快和多少時間或多少。
我認爲這可能與嘗試訪問m_noteList
沒有索引有關。但我沒有檢查由getIndexByID
返回的值,它是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);
}