当删除对象的指针时(删除对象),应用程序随机崩溃

时间:2014-02-28 19:18:26

标签: c++ qt

我的应用程序存在稳定性问题,我无法追溯到它的根目录。我有一个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;
}

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);
}

1 个答案:

答案 0 :(得分:0)

根据您的描述,您的程序看起来有某种内存损坏。我认为我以前的帖子也可能对这个问题有用。

如果您的程序是特定于Windows的,您应该看到以下链接:

https://stackoverflow.com/a/22074401/2724703

如果您的程序特定于Gnu / Linux,您应该看到以下链接:

https://stackoverflow.com/a/22085874/2724703

相关问题