Getting stuck with “segmentation fault” in Qt while using Qt creator to create a very simple project



  • Here are my source code, a super simply one:

    #include<QApplication>
    #include<QLabel>
    #include<QDialog>
    
    int main(int argc,char* argv[])
    {
       QApplication app(argc,argv);
        QDialog* dialog=new QDialog;
       QLabel *label=new QLabel(dialog);
       QLabel->setText("Hello world");
       QDialog->show();
      // label->show();       it the same with or without this
      
     return app.exec();
    }
    

    When I build it, all is good. But when I try to run it, it immediately stop, telling me that "*.exe have already stop. close it or debug it.".

    So I try to debug it. After a while, it throw out a message:

    The inferior stopped because it received a signal from the operating system
    
     SINAL NAME: SIGSEGV
     SIGNAL MEANING: segmentation default
    

    I went check a lot on google and stackoverflow and wikipedia. Many people say that it is caused by the wrong use of pointer. But I can't find out where the problem is. Please help.

    By the way, I'm using win7 and I have try it on qt 4.8.5 and qt 5.4.0.
    The compilers and debuggers are good(at least the qt creator show no error).

    Should I reinstall Qt or else?

    Here is wrong code(in qatomic_i386.h) the debugger point out for me:

    inline bool QBasicAtomicInt::deref()
    {
            unsigned char ret;
        asm volatile("lock\n"
                     "decl %0\n"
                     "setne %1"
                     : "=m" (_q_value), "=qm" (ret)
                     : "m" (_q_value)
    
                     : "memory");   //this is the wrong code the debugger point out for me
                                    
     return ret != 0;
    }
    

    Another line in qstring.h:

    inline QString::~QString() { if (!d->ref.deref()) free(d); }
    

    That's all wrong information. Thanks.



  • Hi,
    change this:

    QLabel->setText("Hello world");
    QDialog->show();
    

    to this:

    label->setText("Hello world");
    dialog->show();
    

    as you should use your variables not class types.



  • Hi
    Replace
    QLabel-> and QDialog-> with
    label-> and dialog->

    HTH



  • @artwaw Oh I put the wrong just now. I did as you said, I just type it wrong.



  • @walkerlala Well, it compiles and runs without any problems on my machine. You can use built-in debugger to exec your app step by step and see when it crashes.



  • The example that was posted will not compile. It is not a copy and paste from the program you are running. There may be other differences that cause the crash.

    The test program is also unusual. Typically you would create a subclass of QDialog and not create an instance of QDialog directly (nothing will be in the dialog - what would this be used for?).

    The label (child of the dialog) would normally be created in the constructor for the parent widget. What you have looks fine but is unusual. The label wont be placed properly and may not even be visible.

    If your sample was reduced to simply creating an instance of QLabel, without the dialog, and setting the text of the label I expect no problems.



  • @Rondog I am sorry Rondog but it does compile. It is wrong written even as a test program, it throws out warnings about software not being able to determine right window and widget geometry but it does compile and it does run.



  • I am surprised. I would expect that

    QLabel->setText("text");
    

    would not compile. I didn't try this obviously. I would expect the compiler would reject this.

    If it does compile that doesn't make it right. This is probably where the crash comes from in this case.



  • @Rondog QLabel->setText does not compile, that's true.
    But, if I understood well, that was corrected to actual variable name. The same goes for QDialog / dialog.



  • That was my point. Your sample program that was posted won't compile so it is not possible to see the problem with your compiled version (it must be different).

    You can only manipulate the instance of a class/structure/object. You cannot call any (non-static) members of QLabel directly.

    I would be surprised if this example doesn't work:

    #include<QLabel>
    
    int main(int argc,char* argv[])
    {
       QApplication app(argc,argv);
    
       QLabel *label=new QLabel; 
       label->setText("Hello world");
       label->show(); 
      
     return app.exec();
    }

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.