PDA

View Full Version : segmentation fault on classes



liran ritkop
6th April 2011, 15:47
Hi all!
I have a weird issue:
I use the QStackedWidget, and i place inside it objects of a class that declared like this:

class ScreenHead: public QWidget
{
Q_OBJECT
...

Now, ScreenHead also has some members, and one of it is another QStackedWidged that contains several objects of this class:

class menu_screen : public QGraphicsView
{
Q_OBJECT
...

Well, the problem is, that when i do that:

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:

stemp = (ScreenHead*) firstLevelStackedWidget->currentWidget();
qDebug() << "stemp =" << stemp;
i get:

stemp = menu_screen(0x1dbdd8)

I don't understand why is that and what is wrong?
someone can help me?

JohannesMunk
6th April 2011, 15:55
You should use


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

nightghost
6th April 2011, 16:01
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."

liran ritkop
6th April 2011, 16:11
I did this, but when i compile, i get this error:

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'

nightghost
6th April 2011, 16:14
Please show the corresponding piece of code

JohannesMunk
6th April 2011, 16:17
@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

nightghost
6th April 2011, 16:21
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

JohannesMunk
6th April 2011, 16:26
@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

liran ritkop
6th April 2011, 16:27
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";

nightghost
6th April 2011, 16:28
the object_cast is a template function, try this:



stemp = qobject_cast<ScreenHead*>(slidingStacked->currentWidget());

liran ritkop
6th April 2011, 16:30
When i added the brackets it compiled.. thanks
I guess Template needs a definite argument after it?

JohannesMunk
6th April 2011, 16:30
Try it with additional parentheses:


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

liran ritkop
6th April 2011, 16:31
ok, i get

stemp casting failed //as in the qDebug()
what should i do?

JohannesMunk
6th April 2011, 16:36
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

liran ritkop
7th April 2011, 09:55
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:

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?

nightghost
7th April 2011, 10:00
Obviously slidingStacked->currentWidget() is not a ScreenHead*. :) Maybe there is no current widget at all?

Try


qDebug() << slidingStacked->currentWidget();


to see a bit more about the the current widget in the stack

liran ritkop
7th April 2011, 10:08
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?

nightghost
7th April 2011, 10:16
qobject_cast (http://doc.qt.nokia.com/latest/qobject.html#qobject_cast) vs static_cast (http://www.cplusplus.com/doc/tutorial/typecasting/)