Results 1 to 4 of 4

Thread: Function call from a NULL this object ?

  1. #1
    Join Date
    Nov 2010
    Posts
    63
    Thanks
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Function call from a NULL this object ?

    Hi all,

    so my program produced a crash and I ran the debugger. The crash occurs in the following code segment:

    Qt Code:
    1. void RFormulaData::setSpecies(int nr, const RFormSpecies& sp0)
    2. {
    3. if ((nr < 0) || (nr > 5)) return;
    4. sp[nr] = sp0; // line with the crash
    5. }
    To copy to clipboard, switch view to plain text mode 

    The debugger shows the watches seen in the attached screenshot. And this is what confuses me. Apparently the this pointer whose function has already been called is NULL. I've never seen this before. I thought I would get a crash if I tried to call a function from any object pointer that is NULL and never make it into that function.

    The data itself is pretty harmless, I think. 'sp' is just a statically allocated array of type RFormSpecies and size 6.

    Can someone explain how I can be inside the function of a null object, or direct me to an explanation, and give me hint how to solve this problem ?
    Thank you !
    Attached Images Attached Images
    Hint: Think carefully before picking a user name.

  2. #2
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Function call from a NULL this object ?

    Since the problem is in a Qt metacall, I'd guess your project isn't being built correctly. Make sure your headers and source files are listed in your project file, that you don't have any implementation in your headers, and that you've done a 'make clean' and a complete rebuild on your project.

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

    Computer Hater (24th September 2011)

  4. #3
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Function call from a NULL this object ?

    There is "no problem" in calling an member function thru a null pointer, because the "member" function isn't an actual member of an object, the compiler just passes this pointer as a first parameter of the function.
    You get into problems only when you try to access some members of that NULL this...
    But if you do this "trick" it is a sign that some member function should be declared static or not member at all.
    Example this code should work:
    Qt Code:
    1. class A{
    2. public:
    3. A(){};
    4. int sum(int x, int y) { return x + y;}
    5. };
    6. int main()
    7. {
    8. A* p = NULL;
    9. std::cout << p->sum(1, 1); //here the compiler passes 'p' (the address of the object - as a 'this')
    10. return 0;
    11. };
    To copy to clipboard, switch view to plain text mode 

    And for your actual problem see what data your function access and how is your function called with null pointer the problem is most likely at that end.

  5. The following user says thank you to Zlatomir for this useful post:

    Computer Hater (24th September 2011)

  6. #4
    Join Date
    Nov 2010
    Posts
    63
    Thanks
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Function call from a NULL this object ?

    Quote Originally Posted by SixDegrees View Post
    Since the problem is in a Qt metacall, I'd guess your project isn't being built correctly. Make sure your headers and source files are listed in your project file, that you don't have any implementation in your headers, and that you've done a 'make clean' and a complete rebuild on your project.
    Thank you for the reply.

    Actually, I cut the complete function call chain for the screenshot to match the watch list size. It doesn't start where it seems to in the screenshot.

    I did a make clean, but I still get the problem.

    There are both headers and source files in the project for all classes that have both. Only one class has implementations in the header file because it is a template class and I had problems keeping the implemenations in the source file and I believe someone in this forum told me I had to keep them directly in the header, so I did that and it worked.

    Quote Originally Posted by Zlatomir View Post
    But if you do this "trick" it is a sign that some member function should be declared static or not member at all.
    Thank you very much for the info. But I don't understand what you mean by this statement.

    Quote Originally Posted by Zlatomir View Post
    And for your actual problem see what data your function access and how is your function called with null pointer the problem is most likely at that end.
    Okay, so I guess it's a "regular" problem after all.
    Actually I have a strong suspicion now where the error may be. I made some minor changes to that part since it last worked correctly and haven't adjusted everything. Except that now I don't understand why it worked previously. :-D
    Anyway, that's a much more sympathetic problem. :-)

    Thanks again !

    EDIT: When I wrote the response to SixDegrees I didn't have an idea yet where the problem might be, so the response reads as if the problem was still completely unclear and I listed all the things I could rule out as causes.
    Last edited by Computer Hater; 24th September 2011 at 17:18.
    Hint: Think carefully before picking a user name.

Similar Threads

  1. cannot call member function without object
    By been_1990 in forum Qt Programming
    Replies: 11
    Last Post: 23rd October 2010, 18:12
  2. Check if a object is null and destroy objects
    By ruben.rodrigues in forum Newbie
    Replies: 3
    Last Post: 2nd July 2010, 11:25
  3. Replies: 2
    Last Post: 7th July 2009, 18:44
  4. Cannot call function without object
    By Salazaar in forum Newbie
    Replies: 5
    Last Post: 11th June 2007, 15:55
  5. Replies: 3
    Last Post: 16th May 2007, 12:07

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.