[Solved] Segmentionerror with QString
-
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
-
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?
-
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
-
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())?
-
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(); 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;
-
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.
-
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...
-
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 sayd_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.
-
Yeah, just don't forget to delete it somewhere because you're leaking memory now.