Results 1 to 6 of 6

Thread: Exception safety with qt

  1. #1
    Join Date
    Feb 2012
    Posts
    8
    Qt products
    Qt4
    Platforms
    Windows

    Default Exception safety with qt

    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:

    Qt Code:
    1. std::unique_ptr<QTreeView> tree(new QTreeView());
    2. QLayout layout;
    3. layout.addItem(tree.release());
    To copy to clipboard, switch view to plain text mode 
    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)?

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

    Default Re: Exception safety with qt

    Quote Originally Posted by paul23 View Post
    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.

    Qt Code:
    1. std::unique_ptr<QTreeView> tree(new QTreeView());
    2. QLayout layout;
    3. layout.addItem(tree.release());
    To copy to clipboard, switch view to plain text mode 
    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.
    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.


  3. #3
    Join Date
    Feb 2012
    Posts
    8
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Exception safety with qt

    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).

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

    Default Re: Exception safety with qt

    Quote Originally Posted by paul23 View Post
    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.
    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.


  5. #5
    Join Date
    Feb 2012
    Posts
    8
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Exception safety with qt

    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).

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

    Default Re: Exception safety with qt

    And what's the problem about it? If you want to catch exceptions, you are free to do it in your code.
    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.


Similar Threads

  1. Thread safety
    By BalaQT in forum Qt Programming
    Replies: 6
    Last Post: 26th July 2011, 15:58
  2. Safety critical systems and qt
    By yagabey in forum Qt Programming
    Replies: 2
    Last Post: 8th April 2009, 14:11
  3. QWidget and thread safety
    By spraff in forum Qt Programming
    Replies: 3
    Last Post: 22nd January 2009, 01:11
  4. QPointer and thread safety
    By Nb2Qt in forum Qt Programming
    Replies: 1
    Last Post: 22nd August 2008, 09:22
  5. QFile and thread-safety
    By Raistlin in forum Qt Programming
    Replies: 2
    Last Post: 31st January 2008, 15:42

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
  •  
Qt is a trademark of The Qt Company.