PDA

View Full Version : Function call from a NULL this object ?



Computer Hater
24th September 2011, 15:44
Hi all,

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


void RFormulaData::setSpecies(int nr, const RFormSpecies& sp0)
{
if ((nr < 0) || (nr > 5)) return;
sp[nr] = sp0; // line with the crash
}

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 !

SixDegrees
24th September 2011, 16:06
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.

Zlatomir
24th September 2011, 16:13
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:

class A{
public:
A(){};
int sum(int x, int y) { return x + y;}
};
int main()
{
A* p = NULL;
std::cout << p->sum(1, 1); //here the compiler passes 'p' (the address of the object - as a 'this')
return 0;
};

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.

Computer Hater
24th September 2011, 17:07
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.



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.



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.