0

I have a class member:

QSet<QDialog*>* dialogs_;  

Do I need to delete just dialogs_ or do I have to call delete on each element of it as well?

Mat
  • 202,337
  • 40
  • 393
  • 406
user336635
  • 2,081
  • 6
  • 24
  • 30
  • You have to provide some context. If your class only ever initializes `dialogs_` to `NULL` then no, you don't need to `delete` anything. Does it do something else? We don't know, you haven't told us. – ta.speot.is Dec 17 '11 at 09:26

3 Answers3

2

This will do the trick:

qDeleteAll(*dialogs_);
delete dialogs_

You can also do it without dereference:

qDeleteAll(dialogs_->begin(), dialogs->end());
delete dialogs_
Kamil Klimek
  • 12,884
  • 2
  • 43
  • 58
1

Yes, you need to somehow manually delete each QDialog in dialogs_, if it has any.

You can iterate through the QSet and delete them manually yourself. Because QDialog inherits from QWidget, another way is to simply delete the parent of all the dialogs if the parent is allocated on the free store as well, which will in turn delete them.

Note that there's no reason to allocate QSet on the free store, if that's what you're doing. You can save a new/delete operation by simply making it a direct member of your class.

QSet<QDialog*> dialogs_;

That's one less thing you have to worry about w.r.t. manual deletion.

In silico
  • 51,091
  • 10
  • 150
  • 143
0

First iterate through your set, delete every object in it, and then delete the set object.

However, take a note that Qt has it's own memory management, and it might be ok just to delete the set, and leave the objects in it to be destructed by the Qt's mechanism.

BЈовић
  • 62,405
  • 41
  • 173
  • 273
  • I haven't used Qt for a while now, but I'm pretty sure that when you pass a ui element such as a `QDialog` to a parent object, that object becomes responsible for the memory management of the child ui element. Don't quote me on that though. – Chris Parton Dec 17 '11 at 10:30
  • @ChrisParton It is, however if you delete the child, the qt system register that. See [this](http://stackoverflow.com/questions/2491707/memory-management-in-qt) and [this](http://developer.qt.nokia.com/doc/qt-4.8/objecttrees.html) – BЈовић Dec 17 '11 at 10:59