PDA

View Full Version : new & delete usage.



SteveH
13th March 2009, 17:57
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 ?

fnmblot
13th March 2009, 18:44
You could use setAttribute(Qt::WA_DeleteOnClose) for the window and then not have to worry about it. :)

wysota
13th March 2009, 22:50
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.