PDA

View Full Version : segmentation fault



mattia
5th November 2007, 14:57
Hello, i get a segmentation fault when i emit a signal, down here there are some snippet of code, maybe somebody can give me a hint.
Thanks

clientsocket.h


#include <QTcpSocket>
#include <QtGui>
#include "callApplication.h"

class ClientSocket : public QTcpSocket
{
Q_OBJECT

private slots:
void generateError();
void readClient();

private:
CallApplication *callApplication;

};


clientsocket.cpp


ClientSocket::ClientSocket(QTextBrowser *textBrowser)
{
connect(this->callApplication, SIGNAL(error()), this, SLOT(generateError())); //<-- segmentation fault
connect(this, SIGNAL(readyRead()), this, SLOT(readClient()));
}

ClientSocket::readClient(){
//read something from client...
callApplication = new CallApplication(this->textBrowserPark);
}


callApplication.h


#include <QProcess>

class CallApplication : public QObject
{
Q_OBJECT
signals:
void error();
private:
performApplication();
public:
QProcess process;
};


callApplication.cpp


void CallApplication::performApplication()
{
emit error();
}

jpn
5th November 2007, 15:02
Nothing has been assigned to "this->callApplication" by the time of connect(). In other words, it's an uninitialized pointer pointing to random memory garbage.

DeepDiver
5th November 2007, 15:04
this->callApplication is uninitialized - pointer points to somewhere.

You shall always initialize pointers:


ClientSocket::ClientSocket(QTextBrowser *textBrowser)
:callApplication(NULL)
{
...


Second:
You need to pass a valid object to connect.

DeepDiver
5th November 2007, 15:05
Nothing has been assigned to "this->callApplication" by the time of connect(). In other words, it's an uninitialized pointer pointing to random memory garbage.

:D Again too slow! :D

mattia
5th November 2007, 15:13
i got it, but if i try to do in this way
clientSocket.cpp


ClientSocket::ClientSocket(QTextBrowser *textBrowser):callApplication(NULL)
{
//callApplication = new CallApplication(this->textBrowserPark);
connect(this->callApplication, SIGNAL(error()), this, SLOT(generateError()));
}


in this way i don't have the segmentation fault but this error:
QObject::connect: Cannot connect (null)::error() to ClientSocket::generateError()
anyway when i emit the error signal the callApplication class is already initialized cos i have called the readClient() method...

DeepDiver
5th November 2007, 15:16
move the connect statement to your readClient()-method then.

mattia
6th November 2007, 07:07
Done. Now it's ok. Thanks so much!

mattia
6th November 2007, 09:11
Hello, i'm here again with another problem like last one, when the "error" signal is emitted into a slot called by a signal generated from QProcess i can't hook it. Down here the code.
Thanks

clientsocket.h


#include <QTcpSocket>
#include <QtGui>
#include "callApplication.h"

class ClientSocket : public QTcpSocket
{
Q_OBJECT

private slots:
void error();
void readClient();

private:
CallApplication *callApplication;

};


clientsocket.cpp


ClientSocket::ClientSocket(QTextBrowser *textBrowser)
{
callApplication = new CallApplication(this->textBrowserPark);
connect(callApplication, SIGNAL(error(const QString)), this, SLOT(generateError(const QString)));
connect(this, SIGNAL(readyRead()), this, SLOT(readClient()));
}

ClientSocket::readClient(){
//read something from client...
callApplication->performApplication();
}

void ClientSocket::error(QString error)
{
//send a string to the client
}


callApplication.h


#include <QProcess>

class CallApplication : public QObject
{
Q_OBJECT
public:
CallApplication();
signals:
void error();
private:
performApplication();
private slots:
void updateError();
void processFinished(int exitCode, QProcess::ExitStatus exitStatus);
void processError(QProcess::ProcessError error);
public:
QProcess process;
};


callApplication.cpp


CallApplication::CallApplication()
{
connect(&process, SIGNAL(readyReadStandardError()), this, SLOT(updateError()));
connect(&process, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(processFinished(int, QProcess::ExitStatus)));
connect(&process, SIGNAL(error(QProcess::ProcessError)), this, SLOT(processError(QProcess::ProcessError)));
}

void CallApplication::performApplication()
{
emit error(); //this is working correctly, signal connected into clientsocket.cpp
}

void CallApplication::processFinished(int exitCode, QProcess::ExitStatus exitStatus)
{
if (exitStatus == QProcess::CrashExit) {
} else if (exitCode != 0) {
emit error("Qprocess ended"); //it dosen't work, neither the signals emit into processError, signal connected into clientsocket.cpp
}
}

DeepDiver
6th November 2007, 09:28
You have defined a signal error().
The definition of error(const QString&) is missing.

mattia
6th November 2007, 09:39
yes you are rigth i've forgotten a slice of code, now, into callApplication.h on line 9 i'added void error(QString error); but in my code it was already present, without it i can't hook the signal nowhere.

DeepDiver
6th November 2007, 09:59
Make sure the signature of the signal declaration and the signature of the signal in the connect call are identical.

signal:
void error(QString)

and
connect(...., SIGNAL(error(const QString)), ...)

are not the same.

mattia
6th November 2007, 10:12
ok, i deleted the const attribute...
connect(...., SIGNAL(error(QString)), ...)
but i can't call the slot "error" in the QProcess method:
updateError()
processFinished(int exitCode, QProcess::ExitStatus exitStatus)
processError(QProcess::ProcessError error)

while in the performApplication() i can do it...and they are in the same class.
thx

DeepDiver
6th November 2007, 10:20
Which platform do you use?
Run your application from the command line/console and watch out for warnings/errors there.

Note: on Windows you need to add : CONFIG += console to your project file.
as described here: http://doc.trolltech.com/4.3/qfile.html#open-4

mattia
6th November 2007, 10:26
i'm using linux and when i run it into the shell i get no warnings/errors :confused:

edit:
for example i tried to define another method in classApplication.h and i implemented it into classApplication.cpp, this method print something into the consolle, if i call it into performApplication() it works, if i call it into
updateError()
processFinished(int exitCode, QProcess::ExitStatus exitStatus)
processError(QProcess::ProcessError error)
it doesn't work so i can deduce that it isn't just a signal/slot problem

DeepDiver
6th November 2007, 10:36
Hmmm - hard to tell.
Post the whole project zipped - I will have a look.

mattia
6th November 2007, 10:47
you can find whole code here (http://www.mediafire.com/?bitxkd7z0g8)
thanks so much!

DeepDiver
6th November 2007, 11:16
As far as I can see the slots connected to process are never called.
Looks like you never do anything with process.

In this case it's quite logic that you never get the signal emitted.

mattia
6th November 2007, 11:21
When i call the process, for example RDP i can see the virtual terminal opening on my screen so i think the slot connected to the process are called, I've tried to print a string into the QTextBrowser to see if i'm really jump into that slot and i'm able to print it ...:o

EDIT: maybe the signal is not emitted by callApplication but by QProcess, given that i'm trying to hook a signal (error) that it is emitted by another slot called by QProcess, in this case mi connect definition is not valide. Is it possible?

DeepDiver
6th November 2007, 11:42
Yeah - you are right. rdesktop was not installed on my machine ...... :o

I did enable emitting the signal in callApplication:: processFinished and did add qDebug() statements in callApplication:: processFinished and ClientSocket::generateError().

As far as I can see the signal is emitted correctly.

The message is not sent from server to the client or not displayed in the client gui.

mattia
6th November 2007, 11:49
;)
but in ClientSocket::generateError(QString error) i added
this->textBrowserPark->setHtml("generate error..." + error);
to show in the server QTextBrowser if i'm calling generateError method, and it don't show nothing...this should be put in evidence the fact thet i'm not calling the slot...

DeepDiver
6th November 2007, 12:04
in callApplication:: processfinished remove the setHtml statement.

this statement overwrites the message set by ClientSocket::generateError().

mattia
6th November 2007, 13:03
Nothing new, i had already tried to remove it but it keeps on to not work. :crying:
I added this method, and i call it into callApplication:: processfinished

void CallApplication::test(){
this->textBrowserPark->setHtml("Yeah!");
}

it shuold print a string into the server message box but nothing, i can't call no one functions in callApplication:: processfinished...it looks really strange...any work around?

mattia
7th November 2007, 10:37
I have implemented again the server classes and i fixed my problems, thanks for your support ;)