Results 1 to 5 of 5

Thread: if (x->isNull()) crashes if x is not initialized

  1. #1
    Join Date
    Jan 2012
    Location
    Dortmund, Germany
    Posts
    159
    Thanks
    69
    Thanked 10 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Windows Android

    Default if (x->isNull()) crashes if x is not initialized

    Hi,
    I wanted to use (x->isNull()) to find out, if I can safely use a pointer to something. However, if I use it like this:
    Qt Code:
    1. SomeType *x;
    2. //doing some work, perhaps giving x a new object to point to, perhaps not
    3. if (x->isNull()) return false;
    To copy to clipboard, switch view to plain text mode 
    the program crashes on the if-condition. I can see why: there is no object to get the ->isNull() method from.

    If I use:
    Qt Code:
    1. if (x==0) return false;
    To copy to clipboard, switch view to plain text mode 
    instead, it works.

    Now I have three curious questions:
    1. Is it "legal" (aka: advisable) to use the second approach?
    2. Will *-Declarations ensure that the Variable points to 0 before it gets an object to point to?
    3. Is there any use for ->isNull()?

  2. #2
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: if (x->isNull()) crashes if x is not initialized

    Of course (x == 0) works, because an object has not been created using new operator.
    1. Yes, it's legal, but you have to initialize local variables, eg SomeType *x = 0; or create a new object using new operator.
    2. That's right. You have to check if a pointer is not 0.
    3. If object has been allocated.
    NOTE: all these question are C++ question, but not about Qt.
    PS. see nice C++ reference.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

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

    sedi (8th August 2012)

  4. #3
    Join Date
    Oct 2011
    Posts
    27
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: if (x->isNull()) crashes if x is not initialized

    To stay on Qt, note that Qt has smart pointers ! (QSharedPointer, ...)

  5. The following user says thank you to thomas@itest for this useful post:

    sedi (8th August 2012)

  6. #4
    Join Date
    Jan 2012
    Location
    Dortmund, Germany
    Posts
    159
    Thanks
    69
    Thanked 10 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Windows Android

    Default Re: if (x->isNull()) crashes if x is not initialized

    Thank you, spirit, I will make sure to initialize everything with =0 from now on!
    to 3.:
    If object is allocated, ->isnull() must be false, right? Because there can be nothing that's allocated in Null....

    You've stated correctly, that this is more about a c++ understanding thing, but as the ->isNull() method is belonging to Qt data types I thought it might be correct to ask here, sorry if I'm mistaken!

  7. #5
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: if (x->isNull()) crashes if x is not initialized

    isNull for most of the Qt's classes means that an object has no data.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  8. The following user says thank you to spirit for this useful post:

    sedi (10th August 2012)

Similar Threads

  1. Replies: 5
    Last Post: 17th January 2012, 22:12
  2. Replies: 1
    Last Post: 19th December 2010, 19:08
  3. Why QWindow is not initialized with the NEW keyword?
    By probine in forum Qt Programming
    Replies: 5
    Last Post: 3rd April 2010, 22:28
  4. app crashes again
    By sophister in forum Qt Programming
    Replies: 7
    Last Post: 15th June 2009, 10:01
  5. variable in method not initialized?!
    By frosch in forum Qt Programming
    Replies: 10
    Last Post: 3rd September 2006, 14:09

Tags for this Thread

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.