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. [Solved] Segmentionerror with QString

[Solved] Segmentionerror with QString

Scheduled Pinned Locked Moved General and Desktop
16 Posts 3 Posters 7.7k 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.
  • A Offline
    A Offline
    andreyc
    wrote on last edited by Chris Kawa
    #4
    QString d_user = "user";
    QString d_pass =  "pass";
    QString d_url = "url";
    

    This initialization of the members in a header file won't compile unless you use c++11

    What version of Qt do you use?
    If you create a simple project and in main() create QString and assign it. Does it work?

    1 Reply Last reply
    0
    • D Offline
      D Offline
      DDH87
      wrote on last edited by Chris Kawa
      #5

      I do use c++0x. I don't know any other version ;-)

      But I don't want to assign in the header just initialization. The assigning will be done at a later point in the code, see the rest of the code.

      And the compiles just fine. The compilation isn't the problem. The problem is in runtime.

      Same error in qt4 and qt5.

      Program received signal SIGSEGV, Segmentation fault.
      0x00007ffff6af74d3 in QTextStream::operator<<(QString const&) () from /usr/lib64/libQt5Core.so.5
      
      1 Reply Last reply
      0
      • Chris KawaC Offline
        Chris KawaC Offline
        Chris Kawa
        Lifetime Qt Champion
        wrote on last edited by
        #6

        Hi,
        This doesn't sound like a code error. It looks more like a runtime error.

        What platform are you on and what compiler? Are you sure you're linking to the correct libraries? Are you sure you're not mixing release/debug program and libs or libs from different Qt version than the one you compiled with?

        1 Reply Last reply
        0
        • D Offline
          D Offline
          DDH87
          wrote on last edited by Chris Kawa
          #7

          It is a runtime error.

          I have a Linux x86-64 machine i'm compiling on. I had only one qt version 4.8. If I wanted to compile in 5.1 I had a virtual machine running.

          For this last error id did install Qt5.1 On my normal machine. But it's located in a other location, as you can see in the error's And de qmake for qt5 is qmake-qt5 so it can't be a mix-up.

          Compilers

          g++ (SUSE Linux) 4.8.1 20130909 [gcc-4_8-branch revision 202388]
          Copyright (C) 2013 Free Software Foundation, Inc.
          This is free software; see the source for copying conditions.  There is NO
          warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
          
          qmake --version
          QMake version 2.01a
          Using Qt version 4.8.5 in /usr/lib64
          
          qmake-qt5 --version
          QMake version 3.0
          Using Qt version 5.1.1 in /usr/lib64
          
          1 Reply Last reply
          0
          • A Offline
            A Offline
            andreyc
            wrote on last edited by Chris Kawa
            #8

            Is this error reproducible on a small example?
            Like

            int main(int, char**)
            {
              QString test = "The test";
              QDebug() << test;
              return 0;
            }
            

            If it is reproducible then what is an output of ldd for Qt4 and Qt5 versions

            ldd smallExample
            
            1 Reply Last reply
            0
            • D Offline
              D Offline
              DDH87
              wrote on last edited by
              #9

              No this small test doesn't produce an error. Not in qt4 and qt5. So I think it has to do with the complexity of the code.

              If I have the time I'll see if I get the same error if I use a small class en then assign it.

              1 Reply Last reply
              0
              • Chris KawaC Offline
                Chris KawaC Offline
                Chris Kawa
                Lifetime Qt Champion
                wrote on last edited by Chris Kawa
                #10

                Ok, so it might be a code bug after all.
                Maybe you're just calling these methods on a dead pointer?

                d_connection->setUser(ui->user->text());
                

                Where do you call that? is d_connection a valid pointer? is ui constructed at that point (usually via setupUi())?

                1 Reply Last reply
                0
                • D Offline
                  D Offline
                  DDH87
                  wrote on last edited by Chris Kawa
                  #11

                  I could just say the pointers are valid. But I think the best way is to show you the code. Just the implantations the header are quite simple.

                  //mainwindow.cc
                  #include "mainwindow.h"
                  #include "ui_mainwindow.h"
                  
                  #include <QDebug>
                  
                  MainWindow::MainWindow(QWidget *parent) :
                      QMainWindow(parent),
                      ui(new Ui::MainWindow)
                  {
                      ui->setupUi(this);
                      connect(ui->actionConnect, SIGNAL(triggered()), this, SLOT(showConnect()));
                  }
                  
                  MainWindow::~MainWindow()
                  {
                      delete ui;
                  }
                  
                  void MainWindow::showConnect(){
                      qDebug() << "Calling";
                      ConnectWindow connectw;
                      connectw.setConnection(d_connection);
                      connectw.setModal(true);
                      connectw.exec&#40;&#41;;
                      qDebug() << "Called";
                  }
                  
                  //connectwindow.cc
                  #include "connectwindow.h"
                  #include "ui_connectwindow.h"
                  
                  #include <QDebug>
                  
                  ConnectWindow::ConnectWindow(QWidget *parent) :
                      QDialog(parent),
                      ui(new Ui::ConnectWindow)
                  {
                      qDebug() << "Started construction";
                      ui->setupUi(this);
                  }
                  
                  ConnectWindow::~ConnectWindow()
                  {
                      delete ui;
                      qDebug() << "Deconstructed";
                  }
                  
                  void ConnectWindow::setConnection(Connect *connect)
                  {
                      d_connection = connect;
                  }
                  
                  void ConnectWindow::accept()
                  {
                      qDebug() << "We got accepted";
                      d_connection->setUser(ui->user->text());
                      d_connection->setPass(ui->pass->text());
                      d_connection->setUrl(ui->url->text());
                  }
                  

                  user, pass, url are lineEdit

                  //connect.cc
                  Connect::Connect()
                  {
                  }
                  
                  
                  void Connect::setUser(QString user)
                  {
                      qDebug() << "Writing user: ";
                      qDebug() << d_user;
                      qDebug() << " GO!";
                      d_user = user;
                      qDebug() << user;
                  }
                  
                  void Connect::setPass(QString pass)
                  {
                      d_pass = pass;
                      qDebug() << pass;
                  }
                  
                  void Connect::setUrl(QString url)
                  {
                      d_url = url;
                      qDebug() << url;
                  }
                  

                  In all headers that have a d_connection member. Its made like this

                  Connect *d_connection;
                  
                  1 Reply Last reply
                  0
                  • Chris KawaC Offline
                    Chris KawaC Offline
                    Chris Kawa
                    Lifetime Qt Champion
                    wrote on last edited by
                    #12

                    Sorry, it doesn't really answer my question.
                    d_connection is passed (line 23 in first listing) from MainWindow to ConnectWindow but where is it initialized?

                    d_connection is a pointer so you should initialize it to nullptr either in the constructor initialization list or (since you're using c++11) at the declaration site. The debugger might then very well tell you where's the error (accessing null pointer). If you don't initialize it it's random trash and the bug may occur as crashes in random places.

                    1 Reply Last reply
                    0
                    • D Offline
                      D Offline
                      DDH87
                      wrote on last edited by
                      #13

                      If you see the last line of my post you will see in the header with a pointer.

                      I took your advice and made it a null pointer in ConnectWindow. But it gives the same error as it did before.

                      Now I have MainW that makes an object Connect. That I send to ConnectW by pointer. Then ConnectW is setting values in Connect.

                      Wouldn't be easier to have MainW set the values that ConnectW will return to MainW? I don't think this is the best way of may help in this case...

                      1 Reply Last reply
                      0
                      • Chris KawaC Offline
                        Chris KawaC Offline
                        Chris Kawa
                        Lifetime Qt Champion
                        wrote on last edited by Chris Kawa
                        #14

                        I saw that line but I don't think you understand.

                        This Connect *d_connection; is not initialized. This is merely declared. This will have garbage value until you actually somewhere say d_connection = whatever

                        So what I suspect you're doing is this:

                        //somewhere in MainWindow.h:
                        Connect *d_connection; //this is garbage
                        //line 23 of first listing
                         connectw.setConnection(d_connection); //still garbage
                        //line 23 of second listing:
                        d_connection = connect; //assign garbage to garbage
                        //line 29 of second listing
                        d_connection->setUser(ui->user->text()); //cant' call -> operator on garbage
                        

                        What I said about nullptr is you should either do this

                        //in the header
                        Connect *d_connection = nullptr;
                        

                        or that

                        MainWindow::MainWindow(QWidget *parent) :
                            QMainWindow(parent),
                            ui(new Ui::MainWindow), d_connection(nullptr)
                        {
                          ...
                        

                        This will not "fix" your program. This will just make it more obvious when it breaks. To actually fix this you should initialize d_connect to something useful(I'm talking d_connection = new Connect()) before you pass it to the ConnectWidget.

                        As for what's better for your case - I don't know. I don't know what these classes are and what are they suppose to do. That's your call.

                        1 Reply Last reply
                        0
                        • D Offline
                          D Offline
                          DDH87
                          wrote on last edited by Chris Kawa
                          #15

                          Thnx. you are a saint! it works!

                          I added this to the constructor of MainWindow and it now it works.

                          d_connect = new Connect;
                          
                          1 Reply Last reply
                          0
                          • Chris KawaC Offline
                            Chris KawaC Offline
                            Chris Kawa
                            Lifetime Qt Champion
                            wrote on last edited by
                            #16

                            Yeah, just don't forget to delete it somewhere because you're leaking memory now.

                            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