Results 1 to 18 of 18

Thread: segmentation fault on classes

  1. #1
    Join Date
    Sep 2010
    Location
    Tel Aviv, Israel
    Posts
    26
    Thanks
    2
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11 Windows

    Exclamation 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:
    Qt Code:
    1. class ScreenHead: public QWidget
    2. {
    3. Q_OBJECT
    4. ...
    To copy to clipboard, switch view to plain text mode 

    Now, ScreenHead also has some members, and one of it is another QStackedWidged that contains several objects of this class:
    Qt Code:
    1. class menu_screen : public QGraphicsView
    2. {
    3. Q_OBJECT
    4. ...
    To copy to clipboard, switch view to plain text mode 

    Well, the problem is, that when i do that:
    Qt Code:
    1. ScreenHead* stemp;
    2. menu_screen* mtemp;
    3.  
    4. stemp = (ScreenHead*) firstLevelStackedWidget->currentWidget();
    5. mtemp = (menu_screen*) stemp->secondLevelStackedWidget->currentWidget();
    To copy to clipboard, switch view to plain text mode 

    I got a segmentation fault on it.
    In another place, when i do this:
    Qt Code:
    1. stemp = (ScreenHead*) firstLevelStackedWidget->currentWidget();
    2. qDebug() << "stemp =" << stemp;
    To copy to clipboard, switch view to plain text mode 
    i get:
    stemp = menu_screen(0x1dbdd8)
    I don't understand why is that and what is wrong?
    someone can help me?

  2. #2
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: segmentation fault on classes

    You should use

    Qt Code:
    1. YourClass* c = qobject_cast<YourClass*>(..currentWidget());
    2. if (c) {
    3. ...
    4. } else {
    5. qDebug() << "Cast failed";
    6. }
    To copy to clipboard, switch view to plain text mode 
    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
    Last edited by JohannesMunk; 6th April 2011 at 15:19. Reason: missing ;

  3. #3
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    131
    Thanks
    11
    Thanked 16 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default 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."

  4. #4
    Join Date
    Sep 2010
    Location
    Tel Aviv, Israel
    Posts
    26
    Thanks
    2
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: segmentation fault on classes

    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'

  5. #5
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    131
    Thanks
    11
    Thanked 16 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: segmentation fault on classes

    Please show the corresponding piece of code

  6. #6
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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

  7. #7
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    131
    Thanks
    11
    Thanked 16 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default 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

  8. #8
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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

  9. #9
    Join Date
    Sep 2010
    Location
    Tel Aviv, Israel
    Posts
    26
    Thanks
    2
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: segmentation fault on classes

    Qt Code:
    1. stemp = qobject_cast<ScreenHead*> slidingStacked->currentWidget();
    2. if (stemp){
    3. mtemp = qobject_cast<menu_screen*> stemp->pSliding->currentWidget();
    4. if (!mtemp)
    5. qDebug() << "mtemp casting failed";
    6. }
    7. else qDebug() << "stemp casting failed";
    To copy to clipboard, switch view to plain text mode 

  10. #10
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    131
    Thanks
    11
    Thanked 16 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: segmentation fault on classes

    the object_cast is a template function, try this:

    Qt Code:
    1. stemp = qobject_cast<ScreenHead*>(slidingStacked->currentWidget());
    To copy to clipboard, switch view to plain text mode 

  11. The following user says thank you to nightghost for this useful post:

    liran ritkop (6th April 2011)

  12. #11
    Join Date
    Sep 2010
    Location
    Tel Aviv, Israel
    Posts
    26
    Thanks
    2
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: segmentation fault on classes

    When i added the brackets it compiled.. thanks
    I guess Template needs a definite argument after it?

  13. #12
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: segmentation fault on classes

    Try it with additional parentheses:

    Qt Code:
    1. stemp = qobject_cast<ScreenHead*>(slidingStacked->currentWidget());
    2. if (stemp)
    3. {
    4. mtemp = qobject_cast<menu_screen*>(stemp->pSliding->currentWidget());
    5. if (!mtemp) qDebug() << "mtemp casting failed";
    6. } else qDebug() << "stemp casting failed";
    To copy to clipboard, switch view to plain text mode 

    Aah: Too slow :->

    Joh

  14. The following user says thank you to JohannesMunk for this useful post:

    liran ritkop (6th April 2011)

  15. #13
    Join Date
    Sep 2010
    Location
    Tel Aviv, Israel
    Posts
    26
    Thanks
    2
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: segmentation fault on classes

    ok, i get
    Qt Code:
    1. stemp casting failed //as in the qDebug()
    To copy to clipboard, switch view to plain text mode 
    what should i do?

  16. #14
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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
    Last edited by JohannesMunk; 6th April 2011 at 15:44. Reason: updated contents

  17. #15
    Join Date
    Sep 2010
    Location
    Tel Aviv, Israel
    Posts
    26
    Thanks
    2
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default 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:
    Qt Code:
    1. void MainClass::updateMenuScreenIcon()
    2. {
    3. menu_screen* mtemp;
    4. ScreenHead* stemp;
    5.  
    6. qDebug("111");
    7. stemp = qobject_cast<ScreenHead*> (slidingStacked->currentWidget());
    8. if (stemp){
    9. mtemp = qobject_cast<menu_screen*> (stemp->pSliding->currentWidget());
    10. if (!mtemp)
    11. qDebug() << "mtemp casting failed";
    12. }
    13. else qDebug() << "stemp casting failed";
    14. mtemp->updateScreen();
    15. qDebug("4");
    16. }
    To copy to clipboard, switch view to plain text mode 

    (Don't mention the qDebug functions, i use them for debugging, of course..)
    Do you think of other code that can help?

  18. #16
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    131
    Thanks
    11
    Thanked 16 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: segmentation fault on classes

    Obviously slidingStacked->currentWidget() is not a ScreenHead*. Maybe there is no current widget at all?

    Try
    Qt Code:
    1. qDebug() << slidingStacked->currentWidget();
    To copy to clipboard, switch view to plain text mode 

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

  19. #17
    Join Date
    Sep 2010
    Location
    Tel Aviv, Israel
    Posts
    26
    Thanks
    2
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default 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?

  20. #18
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    131
    Thanks
    11
    Thanked 16 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: segmentation fault on classes


Similar Threads

  1. Segmentation Fault
    By Ryan Riffle in forum Qt Programming
    Replies: 4
    Last Post: 16th January 2011, 20:52
  2. QWT - Segmentation Fault
    By Wojtek.wk in forum Newbie
    Replies: 0
    Last Post: 17th April 2010, 14:29
  3. segmentation fault
    By navid in forum Qt Programming
    Replies: 3
    Last Post: 20th December 2009, 11:40
  4. segmentation fault
    By mattia in forum Newbie
    Replies: 22
    Last Post: 7th November 2007, 10:37
  5. segmentation fault
    By shamik in forum Qt Programming
    Replies: 3
    Last Post: 24th November 2006, 07:33

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.