Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Getting stuck with “segmentation fault” in Qt while using Qt creator to create a very simple project

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

Scheduled Pinned Locked Moved General and Desktop
qtcreatorqt 4.8.5segmentation fa
10 Posts 4 Posters 6.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    walkerlala
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • artwawA Offline
      artwawA Offline
      artwaw
      wrote on last edited by
      #2

      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.

      For more information please re-read.

      Kind Regards,
      Artur

      W 1 Reply Last reply
      0
      • G Offline
        G Offline
        GrahamL
        wrote on last edited by
        #3

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

        HTH

        1 Reply Last reply
        0
        • artwawA artwaw

          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.

          W Offline
          W Offline
          walkerlala
          wrote on last edited by
          #4

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

          artwawA 1 Reply Last reply
          0
          • W walkerlala

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

            artwawA Offline
            artwawA Offline
            artwaw
            wrote on last edited by
            #5

            @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.

            For more information please re-read.

            Kind Regards,
            Artur

            1 Reply Last reply
            0
            • R Offline
              R Offline
              Rondog
              wrote on last edited by
              #6

              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.

              artwawA 1 Reply Last reply
              0
              • R Rondog

                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.

                artwawA Offline
                artwawA Offline
                artwaw
                wrote on last edited by
                #7

                @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.

                For more information please re-read.

                Kind Regards,
                Artur

                1 Reply Last reply
                0
                • R Offline
                  R Offline
                  Rondog
                  wrote on last edited by
                  #8

                  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.

                  artwawA 1 Reply Last reply
                  0
                  • R Rondog

                    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.

                    artwawA Offline
                    artwawA Offline
                    artwaw
                    wrote on last edited by
                    #9

                    @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.

                    For more information please re-read.

                    Kind Regards,
                    Artur

                    1 Reply Last reply
                    0
                    • R Offline
                      R Offline
                      Rondog
                      wrote on last edited by
                      #10

                      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();
                      }
                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved