简体   繁体   English

删除使用“new”创建的对象

[英]Deleting an object created with “new”

Objects from my class Note are stored by keeping a pointer in a list called m_noteList : 我的类Note中的对象通过将指针保存在名为m_noteList的列表中来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): 删除经过(在获得正确的索引idx以识别对象之后):

delete m_noteList[idx].data();

Error message in QtCreator (Debug-mode) QtCreator中的错误消息(调试模式)

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) 操作系统发出的SIGSEGV信号停止了进程(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... 我的解决方案非常脏:为我的对象设置一些deleted -tag,但是我需要在其他所有函数中检查它以忽略该对象...

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. 我意识到一件事:如果我删除一个音符(使用removeAt() ),它将被删除(我通过观看控制台打印输出看到它),但它的gui不会消失。 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). 然后,如果我再次单击该对象的特定按钮以删除该注释,则应用程序崩溃(因为它不再存在,只有gui)。 But if I write this deleteLater before, after deleting it, the gui also disappears and everything seems fine. 但是如果我之前写了这个deleteLater ,在删除它之后,gui也会消失,一切似乎deleteLater Can someone please explain, why? 有人可以解释一下,为什么?

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

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 . QObject父子内存管理:当删除父QObject时,它会自动删除所有子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. QSharedPointer内存管理:当删除引用对象的最后一个QSharedPointer时,该对象将被删除。 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. “手动”内存管理: new一个对象并在合适的时间delete一个对象。 Explained in the C++ standard. 在C ++标准中解释。 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. 我建议在#1和#2之间进行选择,具体取决于您打算如何使用这些对象。 If you plan to share the objects across "parents", use #2, otherwise use #1. 如果您打算在“父母”之间共享对象,请使用#2,否则使用#1。

Given a QList<QSharedPointer<Note>> m_noteList : 给定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. 删除Note对象是由m_noteList.removeAt(idx)完成的,假设没有其他现存的指针副本。 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 . 在共享指针的数据上调用delete 始终是一个错误

I cannot reproduce your issue with removeAt - it will only happen if you delete the Note first, which you're not supposed to do. 我无法使用removeAt重现您的问题 - 只有在您首先delete Note时才会发生这种情况,这是您不应该做的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM