Results 1 to 3 of 3

Thread: new & delete usage.

  1. #1
    Join Date
    Jan 2009
    Location
    Midlands UK
    Posts
    62
    Thanks
    6
    Thanked 9 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default new & delete usage.

    Hi,

    Firstly, how far do I need to take 'delete' in class destructers in Qt ?

    In other systems I've used (Borland & Windows) it was always best to have a matching 'delete' for every instance of a 'new' even if it was not directly used at shutdown (the item had possibly already been destroyed earlier on in the shutdown process).

    In Qt if I have say a class called wigetX that I add as a 'new' in the mainWindow Program, and widgetX itself has a number of 'new' items in it (pushbuttons, labels etc), will calling delete on widgetX in the mainWindow destructor be enough or should I also call delete within the widgetX destructor for each of the 'new' items in widgetX ?

    Secondly, I need to pass data between two threads using a FIFO buffer arrangement. I've already programmed this using a QReadWriteLock to protect shared acess to the data pointers and it works fine, but as I am finding out with Qt a great many functions have already been written and I may be reinventing the wheel - is there a class already out there for this process - possibly QQueue ?

  2. #2
    Join Date
    Jul 2006
    Location
    Atlanta, GA
    Posts
    86
    Thanks
    26
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: new & delete usage.

    You could use setAttribute(Qt::WA_DeleteOnClose) for the window and then not have to worry about it.
    fnmblot
    --------------------------------------
    Gee Ricky, I'm sorry your mom blew up.

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

    Default Re: new & delete usage.

    There is QQueue but it's not thread-safe, you still have to protect it with a mutex.

    As for memory management - a rule of a thumb is that you have to delete every heap-created object that is not a QObject with a parent or is not explicitely mentioned in Qt docs as an object that Qt takes ownership of (like event or drag objects). A second rule of a thumb is that in most situation calling delete explicitely won't hurt.
    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. The following user says thank you to wysota for this useful post:

    SteveH (14th March 2009)

Similar Threads

  1. Very high CPU usage with QTableView and MVC
    By montylee in forum Qt Programming
    Replies: 7
    Last Post: 24th March 2009, 06:14
  2. Replies: 4
    Last Post: 19th February 2009, 11:10
  3. rotate operation and cpu usage
    By ersin.ozkan in forum Qt Programming
    Replies: 2
    Last Post: 30th December 2008, 06:42
  4. Best way for a graphicsitem to delete itself
    By pherthyl in forum Qt Programming
    Replies: 7
    Last Post: 22nd June 2008, 05:57
  5. c++, placement delete upon exception
    By stinos in forum General Programming
    Replies: 6
    Last Post: 31st October 2006, 15:38

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.