×

Discussion Board

Results 1 to 5 of 5

Thread: DeleteLater()

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    4

    DeleteLater()

    Hello,

    I am making a free 2D game, using QGraphicsScene and QGraphicsView (and a number of other components such as QSound and QMovie). Even the main menu is stored in a scene (enabling custom graphics for the menu). So the scene, the view, and the associated layout that displays the view widget are all stored in member variables (in the main widget class of the application). When the player starts a new game, the main menu scene and view is replaced by the first game map and its content. Similarly, when the first map is finished, a new QGraphicsScene and view is instantiated for the second map (and stored in the same member variables for scene and view).

    Before adding the new scene, layout and view, it is required to free the current versions (or there would be memory leak, obviously), so I made a clean function for it:
    Code:
    _layout->deleteLater();
    _view->deleteLater();
    _scene->deleteLater();
    It does not seem to work, it changes the widget to an "empty" one with default background color (on E52, light blue).

    On the other hand, this works perfectly:
    Code:
    delete _layout;
    delete _view;
    delete _scene;
    How is this possible? Some deadlock?

  2. #2
    Super Contributor
    Join Date
    Oct 2009
    Posts
    4,326

    Re: DeleteLater()

    Did you try:
    Code:
    _view->hide();
    _view->deleteLater();
    _scene->deleteLater();
    _layout->deleteLater();
    ?

  3. #3
    Regular Contributor
    Join Date
    May 2010
    Posts
    56

    Re: DeleteLater()

    AFAIK, correct me if I'm wrong, all deleteLater() does adds the object to a cleanup queue to be freed by the message loop next time it runs (and obviously it's also doing some preliminary cleanup in your case) so why would you use deleteLater() over delete obj.

    Isn't it better to just delete it right away unless you can't afford to wait for it?

  4. #4
    Registered User
    Join Date
    Jun 2010
    Posts
    4

    Re: DeleteLater()

    The documentation of deleteLater() says that it's recommended to use it, because it may cause a problem if the object gets removed while there are still messages addressed to it. Since my application uses a message loop, I thought that it might be better to use deleteLater(). Fortunately, according to the tests, everything works fine with the usual delete operator.

    divanov: thanks, I tried it. Unfortunately, the result is the same.

  5. #5
    Super Contributor
    Join Date
    Oct 2009
    Posts
    4,326

    Re: DeleteLater()

    Can you post a source code ?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×