user2366975
user2366975

Reputation: 4710

Deleting an object created with "new"

Objects from my class Note are stored by keeping a pointer in a list called m_noteList:

QList< QSharedPointer<Note> > m_noteList;   

I am creating an object of that class by:

QSharedPointer<Note> note(new Note(this));
m_noteList << note;

Deleting goes by (after getting the right index idx to identify the object):

delete m_noteList[idx].data();

Error message in QtCreator (Debug-mode)

HEAP[MyApp.exe]: 
HEAP: Free Heap block 024FC240 modified at 02500330 after it was freed

And dissambler:

0x777349ab  <+0x0000>         cmpb   $0x0,0x2(%eax)
0x777349af  <+0x0000>         je     0x777349c6
0x777349b1  <+0x0000>         movb   $0x1,0x7774d640
0x777349b8  <+0x0000>         mov    %ecx,0x7774d644
0x777349be  <+0x0000>         int3
0x777349bf  <+0x0000>         movb   $0x0,0x7774d640   //<-- Here is a stop mark
0x777349c6  <+0x0000>         ret
0x777349c7  <+0x0000>         nop
...

I have also tried this:

m_noteList.removeAt(idx);

Which does not work either. SIGSEGV signal emitted by operating system stopped the process (Segmentation fault)

So I really don't know what I do wrong and how to get around it

My solution is quite dirty: Setting some kind of an deleted-tag to my objects, but I need to check this in every other function to ignore that object...

EDIT:

I realised one thing: If i delete one note (with removeAt()), it is deleted (i see it by watching the console print outputs), but its gui does not disappear. Then if I click again on that object's specific button that is to delete the note, the application crashes (because it does not really exist any more, but only the gui). But if I write this deleteLater before, after deleting it, the gui also disappears and everything seems fine. Can someone please explain, why?

m_noteList.at(idx)->deleteLater();
m_noteList.removeAt(idx);

Upvotes: 0

Views: 182

Answers (2)

deGoot
deGoot

Reputation: 1016

You are mixing up three memory management strategies:

  1. QObject parent-child memory management: when a parent QObject is deleted, it automatically deletes all child QObjects. Explained here. Deleting the object is done automatically, so explicit deletion is not necessary.

  2. QSharedPointer memory management: when the last QSharedPointer referring to an object is deleted, the object is deleted. Explained here. The object is deleted automatically, so explicit deletion is not necessary.

  3. "Manual" memory management: new an object and delete an object at the right time. Explained in the C++ standard. The object must be deleted manually.

You should select only one of the above to make sure you don't double-delete objects. I suggest choosing between #1 and #2 depending on how you plan to use the objects. If you plan to share the objects across "parents", use #2, otherwise use #1.

Upvotes: 2

Given a QList<QSharedPointer<Note>> m_noteList:

The deletion of the Note object is accomplished by m_noteList.removeAt(idx), assuming that there are no other extant copies of the pointer. You simply destruct the last shared pointer to a given note, and that's it. Invoking delete on a shared pointer's data is always an error.

I cannot reproduce your issue with removeAt - it will only happen if you delete the Note first, which you're not supposed to do.

Upvotes: 0

Related Questions