[英]Is an object pointer in a vector deleted if I call delete on the object?
[英]App randomly crashing when an object's pointer is deleted (to delete the object)
我的應用程序存在穩定性問題,無法追溯到其根源。 我有一個trayicon菜單,可以從中創建新的筆記。 在筆記中,我可以通過按QToolButton刪除該筆記。
問題:刪除后,應用程序有時會崩潰。 在大多數情況下,當我非常迅速地編譯和創建2個注釋時,立即刪除這些注釋。 此刪除過程僅涉及下面的代碼,但我找不到我在做什么錯。
有時,刪除所有便箋時,它也會崩潰,無論它有多快以及時間或筆記的數量。
我認為這可能與嘗試通過它沒有的索引訪問m_noteList
。 但是我確實檢查了getIndexByID
返回的值,沒關系...
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;
}
注意: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);
}
從您的描述看來,您的程序似乎存在某種內存損壞。 我認為我以前的帖子也可能對這個問題有用。
如果您的程序是Windows特定的,則應該看到以下鏈接:
https://stackoverflow.com/a/22074401/2724703
如果您的程序是特定於Gnu / Linux的,則應該看到以下鏈接:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.