Results 1 to 6 of 6

Thread: How to mix QScopedPointer with QPointer

  1. #1
    Join Date
    Aug 2010
    Posts
    22
    Thanks
    11
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default How to mix QScopedPointer with QPointer

    I have a class like this:
    Qt Code:
    1. class Class1
    2. {
    3. public:
    4.  
    5. Apple * apple();
    6.  
    7. private:
    8. QScopedPointer< QPointer<Apple> > m_apple;
    9. };
    To copy to clipboard, switch view to plain text mode 

    Apple is a QDialog.
    How do I instantiate m_apple in apple()?
    I tried the following but could not compile:
    Qt Code:
    1. Apple * Class1::apple() {
    2. if (m_apple == 0) {
    3. m_apple->reset(new Apple());
    4. }
    5. return m_apple;
    6. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: How to mix QScopedPointer with QPointer

    Can you explain what you actually think this statement does?:
    Qt Code:
    1. QScopedPointer< QPointer<Apple> > m_apple;
    To copy to clipboard, switch view to plain text mode 
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Aug 2010
    Posts
    22
    Thanks
    11
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to mix QScopedPointer with QPointer

    Well, first I want Class1 to automatically delete m_apple on destruction, hence QScopedPointer.
    Then I also want m_apple to be automatically set to 0 when it is deleted, hence QPointer.
    Qt::WA_DeleteOnClose is set to true in Apple.

    Of course I can just "delete m_apple" in destructor, but I want to see if this works.
    Last edited by jezz; 13th April 2012 at 23:12.

  4. #4
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to mix QScopedPointer with QPointer

    right.... so please say what is wrong with

    Qt Code:
    1. QScopedPointer<Apple> m_apple;
    To copy to clipboard, switch view to plain text mode 


    And what do you think will happen if Apple has been deleted (because WA_deleteonclose is true), and the the destructor is called? Hint - double deletion.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  5. #5
    Join Date
    Aug 2010
    Posts
    22
    Thanks
    11
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to mix QScopedPointer with QPointer

    If QScopedPointer is used:
    Qt Code:
    1. QScopedPointer<Apple> m_apple;
    To copy to clipboard, switch view to plain text mode 
    There are two scenarios:
    1. m_apple was closed by user during runtime, and was deleted automatically since WA_deleteonclose is true. When Class1 is deleted, QScopedPointer deletes a dangling pointer since m_apple is not 0. Double-deletion, so runtime error.
    2. m_apple was not closed by user during runtime. When Class1 is deleted, QScopedPointer deletes m_apple automatically. No runtime error since m_apple has not been deleted before.



    If QPointer is used:
    Qt Code:
    1. QPointer<Apple> m_apple;
    To copy to clipboard, switch view to plain text mode 
    There are two scenarios:
    1. m_apple was closed by user during runtime, and was deleted automatically since WA_deleteonclose is true. When Class1 is deleted, m_apple is already deleted, so no memory leak.
    2. m_apple was not closed by user during runtime. When Class1 is deleted, nobody deletes m_apple, memory leak.

    As I said I can just use QPointer and put 'delete m_apple' in Class1::~Class1(), but I want to see if QScopedPointer and QPointer work together.
    So if QScopedPointer and QPointer is combined:
    Qt Code:
    1. QScopedPointer < QPointer<Apple> > m_apple;
    To copy to clipboard, switch view to plain text mode 
    There are two scenarios:
    1. m_apple was closed by user during runtime, and was deleted automatically since WA_deleteonclose is true. When Class1 is deleted, QScopedPointer should delete a QPointer which is already 0, which is OK, and no double-deletion.
    2. m_apple was not closed by user during runtime. When Class1 is deleted, QScoped deletes m_apple.
    Last edited by jezz; 13th April 2012 at 23:11.

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

    Default Re: How to mix QScopedPointer with QPointer

    I completely I completely fail to see the point of deleting the Apple instance when it is closed if next time you call apple(), it will be recreated again. Especially that if the dialog contains any valuable information, you won't be able to access it once the dialog is closed (and thus destructed).I completely fail to see the point of deleting the Apple instance when it is closed if next time you call apple(), it will be recreated again. Especially that if the dialog contains any valuable information, you won't be able to access it once the dialog is closed (and thus destructed).

    The simplest thing you can do is this:

    Qt Code:
    1. class Class1 {
    2. const Apple * apple() const { return &m_apple; }
    3. private:
    4. Apple m_apple;
    5. };
    To copy to clipboard, switch view to plain text mode 

    or just set a parent for the dialog that will delete it when it itself is getting deleted.
    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. QPointer gives compilation errors.
    By pkj in forum Qt Programming
    Replies: 9
    Last Post: 7th September 2011, 08:18
  2. Replies: 0
    Last Post: 22nd June 2010, 20:27
  3. How to cast QPointer<T> to QPointer<childT>
    By cafu in forum Qt Programming
    Replies: 4
    Last Post: 19th March 2010, 10:51
  4. Problem using QPointer
    By weaver4 in forum Newbie
    Replies: 8
    Last Post: 20th February 2010, 05:05
  5. QMutableVectorIterator and QPointer?
    By Scorp2us in forum Qt Programming
    Replies: 1
    Last Post: 8th November 2008, 19:39

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.