Results 1 to 6 of 6

Thread: QTestLib doesn't recognize NULL pointers?

  1. #1
    Join Date
    Sep 2011
    Posts
    14
    Thanks
    5
    Thanked 7 Times in 7 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QTestLib doesn't recognize NULL pointers?

    Hello, i have a strange issue.

    I'm having a testclass in which i tried to make a test fail, by letting an Object Pointer point to NULL and after that, calling a method on that Pointer like this...

    Qt Code:
    1. HelloWorld *obj = NULL;
    2. obj->hello()
    3. QCOMPARE(obj->hello(), QString("Hello World!"));
    To copy to clipboard, switch view to plain text mode 

    Actually, the above is the code of my first test method.
    the strange is, that it never fails... compiler compiles, and after running it, it says always PASSED.

    what am i understanding wrong??

  2. #2
    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: QTestLib doesn't recognize NULL pointers?

    That's hardly possible, this code should crash. Show your actual code. If that's the actual code then the only explanation is that it is never ran.
    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
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QTestLib doesn't recognize NULL pointers?

    It depends on what you are doing in the hello() method, if you don't access the object's data members, the code can run without a crash. For example, this does not crash (g++ 4.5.2):
    Qt Code:
    1. #include <iostream>
    2.  
    3. class Test{
    4. public:
    5. void test(){
    6. std::cout << "method called\n";
    7. }
    8. };
    9.  
    10. int main(){
    11. Test * t = NULL;
    12. t->test();
    13. return 0;
    14. }
    To copy to clipboard, switch view to plain text mode 

  4. The following user says thank you to stampede for this useful post:

    entonjackson (12th October 2011)

  5. #4
    Join Date
    Sep 2011
    Posts
    14
    Thanks
    5
    Thanked 7 Times in 7 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTestLib doesn't recognize NULL pointers?

    Ok, this sounds logical.
    I'm accessing nothing class-specific in that method. I'm just returning a new QString Object...

    Qt Code:
    1. QString HelloWorld::hello()
    2. {
    3. return "Hello World!";
    4. }
    To copy to clipboard, switch view to plain text mode 

    So... i've just tested it with this:

    Qt Code:
    1. QString HelloWorld::hello()
    2. {
    3. _string = "Hello World!";
    4. return _string;
    5. }
    To copy to clipboard, switch view to plain text mode 

    ...while _string is a member of HelloWorld class. And now it crashes, like it should!

    Wondering if the compiler is optimizing the return "Hello World"; from the first hello() method out, and that is the reason no crash occures.
    Or what other reason there is, why there's no error at runtime...?

    Thank you!

  6. #5
    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: QTestLib doesn't recognize NULL pointers?

    No crash occurs because the compiler never tries to dereference "this" when inside the hello() method. If you make that method virtual, it will start crashing.
    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.


  7. The following user says thank you to wysota for this useful post:

    entonjackson (12th October 2011)

  8. #6
    Join Date
    Sep 2011
    Posts
    14
    Thanks
    5
    Thanked 7 Times in 7 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTestLib doesn't recognize NULL pointers?

    Ok!
    Thanks a lot!

Similar Threads

  1. qtestlib
    By Jordan in forum Qt Programming
    Replies: 7
    Last Post: 28th September 2010, 11:47
  2. Replies: 1
    Last Post: 23rd July 2010, 13:10
  3. Pointers: NULL vs. 0
    By hackerNovitiate in forum Newbie
    Replies: 6
    Last Post: 2nd February 2010, 16:20
  4. How do I get Qt to recognize the oci driver.
    By yleesun in forum Qt Programming
    Replies: 11
    Last Post: 19th January 2009, 03:50
  5. Getting Qt to recognize plugins...
    By KShots in forum Qt Programming
    Replies: 4
    Last Post: 21st April 2007, 10:05

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.