PDA

View Full Version : Error message: The Application crashed



josecarlosmissias
4th December 2009, 18:52
Hello
I created a small application using QTcpServer for sending remote messages when I compile using QT Creator in debug mode the application runs correctly.
When I build configuring QT Creator to release the program crashes and is shown the following error message "image attached".

I wrote a few lines: "qDebug () <<" msg ..."" in some parts of the program:

...
qDebug() << "6. here OK "; far is correct, this message is displayed on the console

if( msg == SAP_RETR_WELCOME_STRING) {
}

qDebug() << "7. here OK "; this message does not show in console

if I try to print the contents of the msg variable, the program crashes:
qDebug () << "contents of msg is. .." << msg;

wysota
4th December 2009, 19:19
Did you initialize this variable? What is its type anyway?

josecarlosmissias
4th December 2009, 19:29
The following definitions:
ServerThread.h:


....
private slots:
void sendMessage(const QString &msg, const QVariant &data);
void onReadyRead();
void onMessage(const QString &msg, const QVariant &data);
....


ServerThread.cpp:



void ServerThread::onReadyRead() {
if (tcpSocket == NULL) return;
if ( tcpSocket->state() != QAbstractSocket::ConnectedState ) return;

QDataStream in(tcpSocket);
in.setVersion(QDataStream::Qt_4_5);
if (blockSize == 0) {
if (tcpSocket->bytesAvailable() < (int)sizeof(quint32)) return;
in >> blockSize;
reading_message = true;
}
if (tcpSocket->bytesAvailable() < blockSize) return;
QString msgString;
QVariant msgData;
in >> msgString;
in >> msgData;

emit onMessage( msgString, msgData );
reading_message = false;
blockSize = 0;
if (tcpSocket->bytesAvailable() > 0) onReadyRead();
}

void ServerThread::onMessage(const QString &msg, const QVariant &data )
{

qDebug() << "6. here OK ";

if( msg == SAP_RETR_WELCOME_STRING)
{
}
qDebug() << "7. here OK ";

....

wysota
4th December 2009, 19:36
QString (and most Qt classes) is not thread-safe - you can't access the same variable from different threads which you probably do bearing the fact that the thread object doesn't live in the same thread as its run() method. Your slots are executing in the context of the main thread - if you try to access any variables living in the worker thread from there, it is likely to lead to a crash.

josecarlosmissias
4th December 2009, 20:09
That's because I be going QString "msg" by reference?

void onMessage (const QString & msg, const QVariant & data);

how can I fix this?

wysota
4th December 2009, 20:27
The problem might not be with the string. It only reveals itself there. First you have to make sure you know which object lives where and which slot will be executed in which context. How does your run() method look like? Also please show the complete declaration of the thread class.

josecarlosmissias
4th December 2009, 21:18
the code can be attached. "zip"?

wysota
4th December 2009, 23:36
Ok, after a quick look at your code....

The thread object and all objects you create in its constructor do not live in the thread controlled by the object but in the thread in which the object was created. So your socket lives in the main thread and is handled by the main thread and not by your server thread. Move its creation to the run() method. When you do that, you will need a message loop running for the slots to activate. So run the exec() method from run() after you setup all objects - then you won't require that while loop. After you have cleaned up your code see if it still crashes and if so then come back and we'll continue investigating.

josecarlosmissias
9th December 2009, 19:10
Hi wysota,

I found the reason why my program crashes, I'm loading a dll, when I comment the calls to the dll works in release mode. Do you have any tips on this it?
I noticed that when I evoke a function of the DLL, the next line the values of the variables are affected, as if the values are lost.

squidge
9th December 2009, 19:44
Sounds like memory corruption. Possibly wrong calling convention causing stack corruption.

josecarlosmissias
9th December 2009, 19:52
Hi fatjuicymole,

If I compile in debug mode works, just the fact of change to build in release mode, ready aborts.

josecarlosmissias
10th December 2009, 12:01
Considering the explanation of fatjuicymole, follow the instructions of the development of the library in C attached, please could someone confirm me how to translate the types required in QT types. Eg:

in C:


typedef int (WINAPI *OpenDllECF) (BYTE *Ident, int porta, int veloc, LPSTR String);


I transcribed this for QT


typedef int (*OpenDllPorta)(int porta,int veloc, char *str);


correct?

squidge
10th December 2009, 13:14
You should leave the original definition alone so the compiler knows the calling convention (WINAPI) and appropriate parameters. You can't change it without also recompiling the library your calling. You also seem to be completely missing one parameter (Ident).

josecarlosmissias
10th December 2009, 16:38
Ok, solved.