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.
  • D Offline
    D Offline
    DDH87
    wrote on last edited by Chris Kawa
    #1

    Hi,

    I have been trying to change the value of a QString. But every time I get a Segmentionerror. This is not just at writing but at reading to.

    //header.h
    class Connect : public QObject
    {
        QString d_user;
        QString d_pass;
        QString d_url;
       (...)
    

    Assigning a value here tot the sting gives the following compile error(')

    //In file included from connect/connect.cc:1:0:
    connect/connect.h:11:22: error: within this context
         QString d_user = "user";
    

    I have a function I call to change the values. There all the same just other values

    void Connect::setUser(QString user)
    {
        d_user = user;
    }
    

    This gives a segmentation error. When accessing d_user. When I do qDebug() << d_user; It will stop there.
    The error from gdb

    Program received signal SIGSEGV, Segmentation fault.
    QString::operator= (this=0x404918 <_start+16>, other=...) at tools/qstring.cpp:1411
    1411    tools/qstring.cpp: Bestand of map bestaat niet. (File or folder doesn't exist)
    

    When reading the error is:

    Program received signal SIGSEGV, Segmentation fault.
    QString (other=..., this=0x7fffffffbca0) at ../../src/corelib/tools/qstring.h:726
    726     ../../src/corelib/tools/qstring.h: Bestand of map bestaat niet.
    

    How do I fix this problem?
    I do not call the Qstrings in de constructor that seams to give some errors that the =operator is private.

    Hope you guys/girls can help.

    Daniël

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andreyc
      wrote on last edited by Chris Kawa
      #2

      Could you show a code that is related to this error.

      In file included from connect/connect.cc:1:0: 
      connect/connect.h:11:22: error: within this context
      QString d_user = "user";
      

      Have you included all necessary header files?
      By any chance, is it possible that you have two or more versions of Qt and are mixing the libraries in one project?

      If you declared d_user here

      class Connect : public QObject
      {
          QString d_user;
      

      and then trying to assign to it the constant "user" later in constructor by

          QString d_user = "user";
      

      then you are shadowing you member d_user and leave it unassigned.

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

        Oke, I'll try to keep it as simple as possible.

        My constructor is empty! Everything done by this class will be done by functions accessed externaly. Just have to save some vars and states to make it work.

        I include QString en QDebug (to print the values send and my local values)

        In the header I do this.

        QString d_user;
        QString d_pass;
        QString d_url;
        

        This works file and compiles without errors.
        But when I access any of the varibels I get a segment error. See my opening post. If I write to d_user I get a other sigment error.

        To see if there is a problem with the assignment to a string I have tried in the header.

        QString d_user = "user";
        QString d_pass =  "pass";
        QString d_url = "url";
        

        I get a compile error. Witch I really don't mind. I don't want to do this. But it looks like there is something going on with the assigning of QString. So how do I fix this?

        I don't have my header file with me so I can't post it. As soon as I do I will post it. But I don't think it will help.

        EDIT:

        #ifndef CONNECT_H
        #define CONNECT_H
        
        #include <QObject>
        #include <QString>
        #include <QtNetwork/QNetworkAccessManager>
        #include <QtNetwork/QNetworkRequest>
        
        class Connect : public QObject
        {
            QString d_user;
            QString d_pass;
            QString d_url;
        
        public:
            Connect();
            void setUser(QString user);
            void setPass(QString pass);
            void setUrl(QString url);
        
        };
        
        #endif // CONNECT_H
        

        I call my assign functions with the following code. But please bare in mind that calling the empty variable does segment error too!

        d_connection->setUser(ui->user->text());
        
        1 Reply Last reply
        0
        • 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