1

My program sometimes gives Segmentation Fault at the last statement in main function.

return a.exec();

I think problem is with order of destruction. Qt documentation says that

No QObject is deleted twice, regardless of the order of destruction.

But when I tried following code it gives segmentation fault.

QWidget* first = new QWidget;
QWidget* second = new QWidget(first);
delete first;
delete second;

I know when first is deleted, it deletes its children second also.

So why does Documentation says that order of destruction doesn't matter?

Should we be always careful about deleting child object?

I am downloading a file with QNetworkAccessManager, when file download is completed I want to delete Downloader object which inherits QObject to free memory. But if I delete it causes segmentation fault.

4
  • 3
    You manually deleting the pointer to the object is completely different than QWidget internally destructing the object.
    – crush
    Jul 18, 2013 at 17:36
  • Qt is designed so yo can use RAII objects. You should allocate all widgets on the stack, and store references to them inside parent widgets as attributes (this is sometimes automatically). This way you don't have to worry about memory management for UI elements, and can access all objects all the time.
    – Spidey
    Jul 18, 2013 at 17:41
  • 1
    @Spidey: Storing all widgets on stack is a bad advice. It might make sense to store top-level windows on stack, but that's it.
    – SigTerm
    Jul 18, 2013 at 17:44
  • I'm pretty sure it's the second delete to actually cause the segfault.
    – Shoe
    Jul 18, 2013 at 17:53

2 Answers 2

13

So why does Documentation says that order of destruction doesn't matter?

It means that you don't NEED to delete objects manually. Qt will register object within parent/child hierarchy and kill it when necessary.

Of course, it can't track all pointers you might have stored somewhere. So if you take a pointer to object, then delete object's parent, then attempt to delete pointer you took, that'll be entirely your fault.

Should we be always careful about deleting child object?

No. When you delete child object it will unregister itself from its parent. If you delete second, then delete first, program will work fine. So you should remember that once you killed an object all its children are gone.

By the way, Qt has QPointer class made specifically for this situation, so you should use it instead of raw pointers. QPointer will set itself to zero when object it points to is destroyed.

But if I delete it causes segmentation fault.

It means that your program has a bug you haven't fixed. And most likely (98% possibility) the bug is within your code, and is not Qt's fault. Debug the segfault and see where it happens.


explanation

QWidget* first = new QWidget;
QWidget* second = new QWidget(first);
delete first;
delete second;

When you delete first it also deletes second, because second is its child. However, your pointer second will not be automatically updated. It'll turn into dangling pointer (points at object that is no longer there), and attempt to delete it, will trigger undefined behavior, which, in your case, will crash the program.

If you INSIST on keeping separate pointer for second AND deleting second after first (again), you can use something like this (don't forget to #include <QPointer>):

QPointer<QWidget> first = new QWidget,
    second = new QWidget(first);
delete first;
delete second;

However, there's no real need for that, and normally people simply do this:

QWidget *first = new QWidget,
    *second = new QWidget(first);
delete first;//second is deleted automatically

Normally, in C++ you have to delete every object you allocated (with new) yourself, manually. This is error-prone, requires you to write a destructor, and you might somemetimes forget about it, which will result in memory leak. However, QObject automatically deletes all its children within its destructor, which means it is perfectly okay to do this:

 QObject *a = new QObject();
 QObject *b = new QObject(a);
 b = new QObject(a);
 b = new QObject(a);

As long as you remember to delete top-level object, all its child resources will be automatically freed.

However, Qt cannot track pointers you stored. So if you do something like this:

QObject *a = new QObject(), *b = a;
delete a;

It will not update existing raw pointers. That's standard C++ behavior, by the way.

That's what QPointers are for:

QObject *a = new QObject(), *b = a;
QPointer<QObject> c = a;
delete a; 
//at this point c is set to zero. a and b  still point at previous object locations.
8
  • In my complete program I had only one delete statement, still it causes segfault
    – Deepak
    Jul 18, 2013 at 17:49
  • @Deepak: It means that your program has a bug you haven't fixed. And the bug is yours.
    – SigTerm
    Jul 18, 2013 at 17:49
  • as in my question, it is on return a.exec();
    – Deepak
    Jul 18, 2013 at 17:53
  • @Deepak: If "it works", it doesn't mean there is are no bugs. The bug is still here, and you still haven't fixed it and something else might trigger it later. Before blaming the library, fix your own code first.
    – SigTerm
    Jul 18, 2013 at 17:53
  • 1
    I am not blaming library, I am just trying to find the bug in my program
    – Deepak
    Jul 18, 2013 at 17:54
2

Qt can't control whether or not you attempt to delete an object that has already been destroyed. second was deleted when the first object was destroyed.

However, the documentation does include the case when you delete a QObject using QObject::deleteLater(), which was designed for this case. I can't think of a case when you would delete a QWidget instead of using this method. The only time it wouldn't work is when you're outside the event loop, in which case you shouldn't be destroying/creating widgets anyway.

4
  • I am using delete objname in an event handler, can it be the cause of the bug? Should I use deleteLater() instead of directly deleting it?
    – Deepak
    Jul 18, 2013 at 17:59
  • 2
    @Deepak: Yes it can be. If you kill object within its own even handler, then something that called event handler might still try to access the object later. Using deleteLater makes sense in this case.
    – SigTerm
    Jul 18, 2013 at 18:17
  • @Deepak: SigTerm is correct. As a rule of thumb, either new and delete all QWidgets yourself AND don't use parenting (NOT recommended, as parenting is forced the moment you start using layouts), OR new your QWidgets with parents and use deleteLater when you have to.
    – Phlucious
    Jul 18, 2013 at 18:29
  • using QObject::deleteLater() fixed my bug. Thank you very much both of you @Phlucious and SigTerm
    – Deepak
    Jul 18, 2013 at 18:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.