Andy Shaw

Qt Commercial Support Weekly #12 - To delete or not to delete

2/1/2012 8:24 PM  | Posted by: Andy Shaw

As a great literary developer may have once said, "To delete or not to delete, that is the question." As we know, with C++ we are expected to clean up after ourselves whenever we do something new so that the memory is available and does not cause a leak.  Of course, Qt makes this easier for us to manage as we have the parent-child relationship with QObject based objects.  Therefore, whenever the parent object is deleted it will delete its children for us, thus saving us from having to remember or worry about ensuring they are deleted.  

 

 

Qt doesn't prevent us from deleting objects directly. However, if we have a need to do it, and if you delete an object that has a child, then it will ensure that it is removed from its parent automatically so you don't need to worry about double deletion.  However, there are some circumstances that you need to be careful of when deleting a Qt object directly, particularly when in an event handler or in a slot that is connected to a signal.

 

 

In a case where you have a setup where you call a function on a widget, you wouldn't expect to be able to do a "delete this;" as this would cause a problem when the function returns or if you do something else after that.  The same principle applies when you decide to delete a widget inside an event handler or in a slot that is directly connected to a signal.  What you need to think of, in this case, is whether the widget you are deleting has triggered the event or the signal that is fired, even if it is a case of the event having propagated through the widget (such as a mouse press event).  If this is the case, then you cannot call delete directly on this widget because Qt will expect it to still exist when the control returns back from the event handler or the slot.

 

 

Luckily for us though there is a way to actually delete these widgets in this case, as in QObject there is a function called deleteLater().  What this function does is posts an event that will effectively cause the widget to be deleted as soon as the control returns to the event loop.  So, when the event handler has been finished with or the slot returns, then it will safely delete the widget in question for us.  You can still set the pointer to be 0 right after calling deleteLater() so it is just a case of swapping "delete widget" for "widget->deleteLater()".

 

So if you find yourself experiencing a crash due to a deleted widget, then try using deleteLater() instead and see if it makes a difference.

 

In other news, for those who use the customer portal, a tip came in from a customer.  If you need to submit more than one case with the same details, then first fill it in without a subject set, then click on submit.  Next, add the subject and click submit.  Then, if you use the back button in the browser, it will present you with the form with all the details filled out for you.  Hopefully, that will be of some use to you!

 

Until next week, happy coding :)

Comments:

cialis coupon | 5/17/2012 12:35 AM
xwampu cialis coupon 7114 viagra rgGIQD buy cheap cialis zcPrDL buy viagra online GeZovK cheap viagra 6941

Micheal234 | 5/17/2012 1:10 PM
Wishing you the best of luck for all your blogging efforts. web design Web Design seo

Micheal234 | 5/17/2012 1:10 PM
seo company seo singapore Seo Singapore Seo Company seo seo

Micheal234 | 5/17/2012 1:11 PM
This post give truly quality information. What is acne acne pimples acne scars how to get rid of acne acne treatment get rid of pimples  face for men skincare products


Micheal | 5/17/2012 1:12 PM
Now I feel very confidence by your tips about that, I think you have chosen a great way to write some info on this topic. Thanks for sharing. cheap reseller hosting cheap web hosting virtual server hosting reseller hosting vps server hosting cheap vps hosting vps hosting linux vps hosting belt buckles belt buckle cool belt buckles cool belt buckle western belt buckles western belt buckle hosting web hosting website hosting vps servers domains

Add new comment:

User verification Image for user verification  
     

Tags

Archive

Authors

Pasi Matilainen

Pasi is a Software Specialist working at Digia, Qt Commercial R&D and he concentrates on Mac OS X development. Pasi holds an M.Sc. degree in Information Technology from the Tampere University of Technology, Finland.

Tarja Sundqvist

Tarja is a Senior Software Engineer in the Digia, Qt Commercial Support team. She has been working in Digia for over 10 years in various positions: software development, testing, error management. Now, Tarja is focusing on helping Qt Commercial customers with their daily Qt problems on Windows and Linux platforms. Tarja holds an M.Sc. degree in Information Processing Science from the University of Oulu, Finland.

Akseli Salovaara

Akseli is a Software Specialist at Digia, Qt Commercial R&D and is responsible for the Qt Commercial releases and deliveries. Akseli holds an B.Sc. degree in Information Technology from the University of Applied Sciences in Jyväskylä, Finland.

Samuli Piippo

Samuli is a Software Specialist at Digia, Qt Commercial R&D with a concentration on  embedded Linux and RTOS development. Samuli holds an M.Sc. degree in Information Processing Science from the University of Oulu, Finland.

Katherine Barrios

Katherine is the Marketing Manager at Digia, Qt Commercial. She is responsible for getting the word out about Qt Commercial to the Qt ecosystem and working together with our customers and the Qt community to further extend the love for Qt on desktop and embedded. She was previously employed at Nokia, Qt Development Frameworks as Program Marketing Manager and is based in Oslo, Norway.

Sami Makkonen

Sami is a Senior Product Manager working at Digia, Qt Commercial R&D and he is responsible for the product planning including new feature development and enhancements to existing functionality. Sami holds an M.Sc.(Econ.) degree in Computer Science.

Andy Shaw

Andy is the Head of Support at Digia, Qt Commercial and has been working with Qt and supporting customers using Qt for 11 years.  He thrives on solving customer problems and getting feedback from them.

Tuukka Turunen

Tuukka is the Director of R&D at Digia, Qt Commercial and is responsible for the planning, creation, verification and delivery of the Qt Commercial product. Tuukka holds a M.Sc.(Eng) and Licentiate of Technology degrees in Computer Science.

Qt Commercial Team