PDA

View Full Version : Exception safety with qt



paul23
3rd March 2012, 01:15
Qt work a lot with raw pointers (why? this isn't the 1990 anymore...). But given that fact I'll have to work myself around this. I notice from the documentation that the layout in those cases often take ownership of the pointer.

However what happens if the container fails & (re)throws an exception - ? ie, consider the following mockup:


std::unique_ptr<QTreeView> tree(new QTreeView());
QLayout layout;
layout.addItem(tree.release());
Now I believe that if an exception occurs during QLayout::addItem() the tree WILL be freed? (layout takes immediate responsibility on the tree when function starts)?

wysota
3rd March 2012, 07:47
Qt work a lot with raw pointers (why? this isn't the 1990 anymore...)
Hmm... I didn't know there was something wrong with pointers.



std::unique_ptr<QTreeView> tree(new QTreeView());
QLayout layout;
layout.addItem(tree.release());
Now I believe that if an exception occurs during QLayout::addItem() the tree WILL be freed? (layout takes immediate responsibility on the tree when function starts)?
Qt doesn't throw exceptions.

paul23
4th March 2012, 14:45
uhm I don't see the noexcept keyword in the functions.. And anything that allocates memory (which a layout WILL do) can throw a std::bad_alloc at the weirdest times.

Manual exception safety is THE reason I choose C++ over C# for developing (I like to keep control in my hand instead of relying on a garbage collector which might or might not act during a time critical operation).

wysota
4th March 2012, 16:44
uhm I don't see the noexcept keyword in the functions..
As far as I know noexcept is part of C++11 standard and Qt can be compiled with C++03 compilers.


And anything that allocates memory (which a layout WILL do) can throw a std::bad_alloc at the weirdest times.
Can it? What weirdest times do you have in mind?


Manual exception safety is THE reason I choose C++ over C# for developing (I like to keep control in my hand instead of relying on a garbage collector which might or might not act during a time critical operation).
C++ sucks when it comes to exceptions. C++ compilers suck when it comes to exceptions even more.

paul23
4th March 2012, 23:35
any code using operator new() will cause a std::bad_alloc exception (ie when running out of virtual heap memory, or the heap gets corrupted etc etc).

wysota
5th March 2012, 08:31
And what's the problem about it? If you want to catch exceptions, you are free to do it in your code.