Results 1 to 9 of 9

Thread: Checking if a pointer is dangling

  1. #1
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    387
    Thanks
    101
    Thanked 15 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Checking if a pointer is dangling

    Hello,

    does anyone know a way to detect if a pointer to a subclass of QObject or even a QWidget is dangling?

    Qt Code:
    1. class MyWidget : public QWidget;
    2.  
    3. MyWidget w = new MyWidget(parent);
    4.  
    5. somewhere()
    6. {
    7. delete w;
    8. }
    9.  
    10. somewhereelse()
    11. {
    12. if (w is not dangling) // <-- how to do this?
    13. do something with w;
    14. }
    To copy to clipboard, switch view to plain text mode 

    I know that you can connect the destroyed() signal, but in my case it's very tiresome. It would be much nicer (and safer) if I could somehow tell by the pointer, if it still points to a valid object or not. Is there such a path to take, oh masters of Qt?

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Checking if a pointer is dangling

    Quote Originally Posted by Cruz View Post
    does anyone know a way to detect if a pointer to a subclass of QObject or even a QWidget is dangling?
    See QPointer.

    Quote Originally Posted by Cruz View Post
    Qt Code:
    1. delete w;
    To copy to clipboard, switch view to plain text mode 
    By the way, QObject::deleteLater() is safer to use especially when it comes to widgets.
    J-P Nurmi

  3. The following user says thank you to jpn for this useful post:

    Cruz (15th February 2009)

  4. #3
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    387
    Thanks
    101
    Thanked 15 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Checking if a pointer is dangling

    I just can't say it enough. Qt is the best thing since sliced bread.

  5. #4
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Checking if a pointer is dangling

    u r right man, QT is the best step one can take on the path to "application software humanity"..i m so glad to be a QTarian..are u?

  6. #5
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Checking if a pointer is dangling

    Quote Originally Posted by talk2amulya View Post
    QT is the best step...
    You mean Qt, right?
    J-P Nurmi

  7. #6
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Checking if a pointer is dangling

    totally..u r not a fan of QT? cmon, u r like the GURU of this thing..u've got to be in love with it!

  8. #7
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Checking if a pointer is dangling

    Quote Originally Posted by talk2amulya View Post
    totally..u r not a fan of QT? cmon, u r like the GURU of this thing..u've got to be in love with it!
    Yes, I'm a huge fan of Qt. But my point was that QT refers to Apple's QuickTime. You probably mean Qt, the toolkit by Qt Software/Nokia.
    J-P Nurmi

  9. #8
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    387
    Thanks
    101
    Thanked 15 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Checking if a pointer is dangling

    All the way through studying computer science my fellows and I were always talking, wouldn't it be dreamy if we had a programing language, which has the nice syntax of Java but doesn't have the gay virtual machine, that slows everything down just to check the array boundaries and to clean up the garbage for you? And there you go, Qt is it! You get 99% of what Java offers AND the speed of C++. Hang on, I need to wipe a tear here.

  10. #9
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Checking if a pointer is dangling

    keep on passin the tissue man, cuz this community is gonna get bigger.. and u r so right about the GAY virtual machine of java..its about time that java guys need to stand up and take a bow at QT..and the whole world to recognize the power of QT..here is to QT!

Similar Threads

  1. QPSQL problem
    By LoneWolf in forum Installation and Deployment
    Replies: 60
    Last Post: 4th November 2009, 14:22
  2. Dragging a pointer
    By Cruz in forum Qt Programming
    Replies: 4
    Last Post: 10th July 2009, 03:51
  3. Checking if bad pointer?
    By steg90 in forum Qt Programming
    Replies: 4
    Last Post: 22nd May 2007, 09:06

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.