0

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);
}
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
user2366975
  • 4,350
  • 9
  • 47
  • 87

1 Answers1

0

From your description it looks like your program has some sort of memory corruption. I think my previous post might be useful for this problem as well.

If your program is windows specific, you should see the following link:

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

If your program is Gnu/Linux specific, you should see the following link:

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

Community
  • 1
  • 1
Mantosh Kumar
  • 5,659
  • 3
  • 24
  • 48