segmentation fault on classes
Hi all!
I have a weird issue:
I use the QStackedWidget, and i place inside it objects of a class that declared like this:
Now, ScreenHead also has some members, and one of it is another QStackedWidged that contains several objects of this class:
Well, the problem is, that when i do that:
Code:
ScreenHead* stemp;
menu_screen* mtemp;
stemp = (ScreenHead*) firstLevelStackedWidget->currentWidget();
mtemp = (menu_screen*) stemp->secondLevelStackedWidget->currentWidget();
I got a segmentation fault on it.
In another place, when i do this:
Code:
stemp = (ScreenHead*) firstLevelStackedWidget->currentWidget();
qDebug() << "stemp =" << stemp;
i get:
Quote:
stemp = menu_screen(0x1dbdd8)
I don't understand why is that and what is wrong?
someone can help me?
Re: segmentation fault on classes
You should use
Code:
YourClass* c = qobject_cast<YourClass*>(..currentWidget());
if (c) {
...
} else {
qDebug() << "Cast failed";
}
thus checking the pointer to be nonzero before accessing it.
That way you can easily track where something goes wrong.
Maybe there are widgets of other types in your stack widgets? Or the stackWidget itself is not properly initialized?
HIH
Johannes
Re: segmentation fault on classes
afaik a c-style cast or a static_cast will never fail. It just tells the compiler. "Do the cast! I know what I am doing."
Re: segmentation fault on classes
I did this, but when i compile, i get this error:
Quote:
MainClass.cpp:580: error: cannot resolve overloaded function 'qobject_cast' based on conversion to type 'ScreenHead*'
MainClass.cpp:580: error: expected ';' before 'slidingStacked'
MainClass.cpp:582: error: cannot resolve overloaded function 'qobject_cast' based on conversion to type 'menu_screen*'
MainClass.cpp:582: error: expected ';' before 'stemp'
Re: segmentation fault on classes
Please show the corresponding piece of code
Re: segmentation fault on classes
@nightghost: Yes the cast itself will never fail, but accessing the variable afterwards as he does in line #5 can result in a nasty access violation.
Joh
Re: segmentation fault on classes
Of course. That was the whole initial problem :) You gave already solution before and I just an explanation why the c-style cast is a bad choice
Re: segmentation fault on classes
@Nightghost: Aah, yes! Sorry for misreading you!
@liran: Now let's see some code, so we can get it to compile. Have you checked for missing ";" ?
Joh
Re: segmentation fault on classes
Code:
stemp = qobject_cast<ScreenHead*> slidingStacked->currentWidget();
if (stemp){
mtemp = qobject_cast<menu_screen*> stemp->pSliding->currentWidget();
if (!mtemp)
qDebug() << "mtemp casting failed";
}
else qDebug() << "stemp casting failed";
Re: segmentation fault on classes
the object_cast is a template function, try this:
Code:
stemp = qobject_cast<ScreenHead*>(slidingStacked->currentWidget());
Re: segmentation fault on classes
When i added the brackets it compiled.. thanks
I guess Template needs a definite argument after it?
Re: segmentation fault on classes
Try it with additional parentheses:
Code:
stemp = qobject_cast<ScreenHead*>(slidingStacked->currentWidget());
if (stemp)
{
mtemp = qobject_cast<menu_screen*>(stemp->pSliding->currentWidget());
if (!mtemp) qDebug() << "mtemp casting failed";
} else qDebug() << "stemp casting failed";
Aah: Too slow :->
Joh
Re: segmentation fault on classes
ok, i get
Code:
stemp casting failed //as in the qDebug()
what should i do?
Re: segmentation fault on classes
Well now you know that slidingStacked->currentWidget() returns something that is clearly not a valid ScreenHead*.
Maybe there are some widgets of another type in the stack?
Aah: In your very first post your debug line said it all: your stemp is a menu_screen not a ScreenHead!
If you still can't find the error, post more or all of your code handling those stackWidgets so that we can stop this guesswork here.
Joh
Re: segmentation fault on classes
Well, I have a lot of code files, and i think that the important code is here, So this is the function i use where i have troubles:
Code:
void MainClass::updateMenuScreenIcon()
{
menu_screen* mtemp;
ScreenHead* stemp;
qDebug("111");
stemp = qobject_cast<ScreenHead*> (slidingStacked->currentWidget());
if (stemp){
mtemp = qobject_cast<menu_screen*> (stemp->pSliding->currentWidget());
if (!mtemp)
qDebug() << "mtemp casting failed";
}
else qDebug() << "stemp casting failed";
mtemp->updateScreen();
qDebug("4");
}
(Don't mention the qDebug functions, i use them for debugging, of course..)
Do you think of other code that can help?
Re: segmentation fault on classes
Obviously slidingStacked->currentWidget() is not a ScreenHead*. :) Maybe there is no current widget at all?
Try
Code:
qDebug() << slidingStacked->currentWidget();
to see a bit more about the the current widget in the stack
Re: segmentation fault on classes
Ok!
Thank you all guys!
You really helped me with the problem..
By the way, can someone explain what is the difference between the C-static casting and the "qobject_cast" casting?
Re: segmentation fault on classes