Results 1 to 9 of 9

Thread: Deleting Class causes app crash

  1. #1
    Join Date
    Oct 2008
    Posts
    306
    Thanks
    6
    Thanked 9 Times in 8 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Deleting Class causes app crash

    In this thread, I created my class and made the arrangements for monitoring it.
    How can I delete the class? I can't find a way that will work, myne just crashes the app.
    Qt Code:
    1. void deleteClass(MyClass *class)
    2. {
    3. class->disconnect();
    4. delete class;
    5. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Deleting Class causes app crash

    Did you remove your alarm from the map?

    QMap<QString, Alarm*> map;
    map.remove(name);

    Does disconnect work immediately or are there any queued connections (interthread) or posted messages involved?

    I need a bigger part of your code to be able to help.

    Try to create a minimal working example, that still exhibits the problem!

    Johannes

  3. #3
    Join Date
    Oct 2008
    Posts
    306
    Thanks
    6
    Thanked 9 Times in 8 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Deleting Class causes app crash

    Here's the function that creates instances of my class "Alarm":
    Qt Code:
    1. Alarm *a = new Alarm(file);
    2. alarmList.insert(a->objectName(),a);
    3.  
    4. ADisplay *display = new ADisplay(this); // other class
    5.  
    6. connect(a,SIGNAL(soundAlarm(Alarm*)),display,SLOT(showDisplay(Alarm*))); // when its time I call display
    7.  
    8. connect(display,SIGNAL(quiting()),a,SLOT(quiting())); // when display is closed call Alarm's quiting() slot (custom) that then:
    9.  
    10. connect(a,SIGNAL(quiting(Alarm*)),this,SLOT(remActiveAlarm(Alarm*))); // emit's the quiting() signal passing back to this class the
    11. // object to delete which is:
    12.  
    13. remActiveAlarm(Alarm *a){
    14. alarmList.remove(a->objectName());
    15. a->disconnect();
    16. delete a;
    17. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Deleting Class causes app crash

    I can guess 2 things -
    One, does remove call destructor too ? Check it, am nto sure.
    Second, are you using the Alarm object from where the signal is emitted ? Chances are you are using. So when the code reaches back after the remActiveAlarm() slot, you have a invalid object and hence you get crash.
    As a remedy, trying a->deleteLater().

  5. #5
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Deleting Class causes app crash

    Obviously we still have to guess, how the rest of the code may look like.. We need more. Try to build a small compilable example, that still crashes.

    Johannes

  6. #6
    Join Date
    Oct 2008
    Posts
    306
    Thanks
    6
    Thanked 9 Times in 8 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Deleting Class causes app crash

    Here attached is a small working(crashing) example.
    Attached Files Attached Files

  7. #7
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Deleting Class causes app crash

    Hi there!

    Qt Code:
    1. ~Alarm()
    2. {
    3. delete this;
    4. }
    To copy to clipboard, switch view to plain text mode 
    That is calling the destructor recursively, until a stack overflow happens. The destructor is called, when you delete an object. No reason to delete it again..

    Qt Code:
    1. ~Alarm()
    2. {
    3. }
    To copy to clipboard, switch view to plain text mode 
    You only delete child objects in the destructor..

    Joh

  8. #8
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Deleting Class causes app crash

    Quote Originally Posted by aamer4yu View Post
    One, does remove call destructor too ? Check it, am nto sure.
    It does not, because he is using pointers in his collection. Pointers are not automatically deleted.

    If you want to properly clear a collection (map, hash, list, ..) of pointers you need to:

    Qt Code:
    1. qDeleteAll(list);
    2. list.clear();
    To copy to clipboard, switch view to plain text mode 

    Or one by one, with delete and remove.. as been_1990 is doing..

    Johannes

  9. #9
    Join Date
    Oct 2008
    Posts
    306
    Thanks
    6
    Thanked 9 Times in 8 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Deleting Class causes app crash

    Thanks JohannesMonk. That was the problem.

Similar Threads

  1. Replies: 3
    Last Post: 27th December 2008, 20:34
  2. Replies: 12
    Last Post: 18th September 2008, 16:04
  3. Accessing a class Object by pointer in another class
    By pdoria in forum Qt Programming
    Replies: 2
    Last Post: 14th January 2008, 16:59
  4. Replies: 3
    Last Post: 16th May 2007, 12:07
  5. Signal/slot looking in base class, not derived class
    By georgie in forum Qt Programming
    Replies: 2
    Last Post: 12th May 2006, 08:36

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.