Results 1 to 20 of 29

Thread: Qt deleteLater() force to delete

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Qt deleteLater() force to delete

    Calling processEvents() doesn't trigger deffered deletion. The application really needs to enter the event loop.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  2. #2
    Join Date
    Aug 2011
    Posts
    15
    Thanks
    2

    Default Re: Qt deleteLater() force to delete

    How can I force it? Is there any way?
    Here is the problem:
    There is application that handles text commands. I have a Qt widget that is closed with some close * command. Qt::WA_DeleteOnClose attribute is set for that widget, it receives closeEvent, but destructor for that object is called later (I guess on idle).
    If I have two commands
    close *;
    get something;
    the program crashes because "get something" is called before destructor for that widget, so widget still receives events and it tries to access data deleted by close * command. I would like to force Qt to delete all pending objects after command completion.

    I guess previously it was entering event processing loop, but something is changed.

    Any ideas how to make it work?
    Thanks.
    Last edited by axe; 18th August 2011 at 09:12.

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Qt deleteLater() force to delete

    Quote Originally Posted by axe View Post
    How can I force it?
    Why would you want to force it?

    the program crashes because "get something" is called before destructor for that widget, so widget still receives events and it tries to access data deleted by close * command. I would like to force Qt to delete all pending objects after command completion.
    Then either schedule execution of "get something" and return to the event loop or simply have the closeEvent for the widget (or some other appropriate place) contain code that will set a flag that is checked before the widget tries to access this data. Or use one of available smart pointer classes.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. #4
    Join Date
    Aug 2011
    Posts
    15
    Thanks
    2

    Default Re: Qt deleteLater() force to delete

    wysota

    The problem appeared after updating Qt version from 4.3.3 to 4.7.2. I wouldn't like making such changes, the code is really huge and every simple change may result in thousands of bugs. I would like to fix the problem with minimum amount of changes. I guess previously Qt was forced to delete the object by calling sendPostedEvents() (at least comment says that), but currently this doesn't work.

    Here is the code
    Qt Code:
    1. // Send queued events (including deferred deletions) to all QObjects
    2. // unless we have to preserve view objects
    3. if (deleteImmediately)
    4. QCoreApplication::sendPostedEvents(NULL, -1);
    To copy to clipboard, switch view to plain text mode 

    This code looks strange to me, however this is what we have and this is what was working before.
    Do you mean there is no way to force it?
    Last edited by axe; 18th August 2011 at 10:22.

  5. #5
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Qt deleteLater() force to delete

    May be I didn't get the complete picture yet, if you want to delete immediately, can't you just call delete on the object directly, instead of calling deleteLater() on the object.

  6. #6
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Qt deleteLater() force to delete

    May be I didn't get the complete picture yet, if you want to delete immediately, can't you just call delete on the object directly, instead of calling deleteLater() on the object.
    (few posts above)
    I can replace this function call with manual deletion, but I'm not sure what will happen if Qt will try to delete this deferred object. If I remove DeleteOnClose attribute then I would need to delete all objects manually, but the code is so huge that it's really impossible to do not forget about anything. I wouldn't like making such changes.

Similar Threads

  1. Let QThread delete itself with deleteLater();
    By Lodorot in forum Qt Programming
    Replies: 1
    Last Post: 24th February 2010, 16:48
  2. To use or not to use: deleteLater()
    By codeslicer in forum Qt Programming
    Replies: 11
    Last Post: 11th July 2009, 21:43
  3. Replies: 4
    Last Post: 19th February 2009, 11:10
  4. QT deleteLater again.
    By bunjee in forum Qt Programming
    Replies: 1
    Last Post: 10th May 2008, 22:36
  5. Qt Deletelater
    By bunjee in forum Qt Programming
    Replies: 2
    Last Post: 8th January 2008, 16:58

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
  •  
Qt is a trademark of The Qt Company.