Results 1 to 3 of 3

Thread: Calling destructor for classes with QWidget or QObject as its base class

  1. #1
    Join Date
    Sep 2012
    Posts
    31
    Thanks
    15
    Qt products
    Qt4
    Platforms
    Windows

    Default Calling destructor for classes with QWidget or QObject as its base class

    Hi, has anyone experienced having a segmentation error when deleting pointers in the destructor of a class? This usually happens when I close my application. I created the pointers with the new keyword. These pointers are also classes with a base class of qobject or qwidget. Do I still need to destroy them? or are they automatically destroyed. When I run my app in debug mode, the breakpoints stop in the destructor and does not cause an error, but when I run it normally, it crashes.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Calling destructor for classes with QWidget or QObject as its base class

    Quote Originally Posted by ehnuh View Post
    Hi, has anyone experienced having a segmentation error when deleting pointers in the destructor of a class?
    Sure, if you haven't then you are not trying hard enough
    This usually happens when I close my application. I created the pointers with the new keyword. These pointers are also classes with a base class of qobject or qwidget. Do I still need to destroy them? or are they automatically destroyed.
    Yes. All the rules of C++ memory management apply to programs using Qt: all allocated memory must be freed and you are responsible for ensuring that happens. There are several ways to do this, and not all of them involve explicitly calling delete. In Qt programs if you create a QObject with a QObject parent, or set one later, then the parent QObject owns it and will delete it. Parentless QObjects are the same as any other C++ object and you will have to delete. There are various forms of smart pointer that can handle allocated memory on your behalf.
    When I run my app in debug mode, the breakpoints stop in the destructor and does not cause an error, but when I run it normally, it crashes.
    You are:
    • Deleting something that has already been deleted (double-free)
    • Deleting something and then trying to use it
    • Doing other obscure things

  3. The following 2 users say thank you to ChrisW67 for this useful post:

    ehnuh (22nd November 2012), ravas (18th October 2015)

  4. #3
    Join Date
    Sep 2012
    Posts
    31
    Thanks
    15
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Calling destructor for classes with QWidget or QObject as its base class

    thanks for the info sir!

Similar Threads

  1. Replies: 2
    Last Post: 23rd October 2012, 10:46
  2. Replies: 5
    Last Post: 14th June 2012, 09:35
  3. Replies: 4
    Last Post: 5th April 2012, 13:38
  4. Replies: 1
    Last Post: 30th October 2010, 13:28
  5. Replies: 7
    Last Post: 15th December 2009, 11:01

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.