How to avoid an object of a class to be destroyed when going out of scope in a function
-
Hello,
I'm working with TcpServer and Client on QT4.7
I have a class TcpServer that creates a server and a client. My server connects to a distant client , receives data from it and send it to the distant server via my client. My client connects to a distant server, receives data from it and send it to the distant client via my server (my class is acting as a spy between the distant server and the distant client). Everything works fine when i had this in my main.cpp :int main(int argc, char *argv[]) { QApplication a(argc, argv); TcpServer server; server.connectToNrs(); server.connectToSncs(); return a.exec(); }
and in my TcpServer.cpp :
void TcpServer::connectToNrs() { _serverNrs = new QTcpServer(this); connect(_serverNrs, SIGNAL(newConnection()), this, SLOT(newConnection())); _serverNrs->listen(QHostAddress("192.168.x.x"), port1); qDebug() << "Listening to connection from nrs"; } void TcpServer::connectToSncs() { _socketTechnique = new QTcpSocket(this); connect(_socketTechnique, SIGNAL(readyRead()), this, SLOT(getSNCS())); _socketTechnique->connectToHost(QHostAddress("192.168.x.x"), port2); if(_socketTechnique->waitForConnected(3000) == true) { qDebug() << "Client connected to technical sncs"; } else { qDebug() << "Client not connected to technical sncs"; } } void TcpServer::newConnection() { _socketServer = new QTcpSocket(this); connect(_socketServer, SIGNAL(readyRead()), this, SLOT(getNRS())); _socketServer = _serverNrs->nextPendingConnection(); qDebug() << QString( "Incoming connection from %1" ).arg(_socketServer->peerAddress().toString()); qDebug() << "Server connected to NRS"; }
But now I added an IHM that, when i click on the button launch, would execute the same actions than in my main.cpp if a boolean is true and creates an other client that would connect to and other server if the boolean is false.
my IHM.cpp :
void IHM::on_buttonLaunch_clicked() { ui->buttonLaunch->setEnabled(false); launchApp(); ui->buttonLaunch->setEnabled(true); } void IHM::launchApp() { server = new TcpServer(); if (button ==true) { server->connectToNrs(); server->connectToSncs(); } else { server->connectToNrs(); server->connectToSncs(); server->connectToSNCS2(); } }
now my main.cpp looks like that :
int main(int argc, char *argv[]) { QApplication a(argc, argv); IHM ihm; ihm.show(); return a.exec(); }
And it's not working anymore. I understand that when my server goes out of scope in my function launchApp() it is destroyed and so the execution fails but I can't seem to find a way around this so if someone could help me it's would be very nice. Thank you !
-
@stordd Either make that local variable a class member or allocate it on the heap (but then you have to make sure it is deleted when not needed anymore)...
Update: actually your server is not going out of scope as it is allocated on the heap. There must be something else wrong in your code. -
@stordd Local variable going out of scope is not the issue in your code. You're allocating on the heap, so it is NOT destroyed when the method finishes. It must be something else in your code.
-
@stordd Also, your server is NOT a local variable apparently:
server = new TcpServer();
This would be a local variable:
TcpServer *server = new TcpServer();
-
@stordd said in How to avoid an object of a class to be destroyed when going out of scope in a function:
when i add
TcpServer *server = new TcpServer();in my launchApp() function sometimes my program exi
I didn't say you should do that! This would make server a local variable (would shadow the class member called server) and thus would be wrong!
Please read more carefully.
What I said is: you do not have a local variable in your code, so this is not the source of your problem. It must be something else." sometimes my program exit in my slot getSNCS()" - of cource is does as now you're accessing a not initialised member variable server. So, do not do
TcpServer *server = new TcpServer();
this is correct
server = new TcpServer();
-
@stordd You need to debug your app. For example: what happens in server->connectToNrs() and server->connectToSncs()?
-
@jsulm It's weird I'm debugging step by step and when it'sdoing the
server.connectToSncs()
andserver.connectToNrs()
it goes to the moc_IHM.cpp atint IHM::qt_metacall()
and then go back to the main.cpp and get stuck on the linea.exec()
And my functions connectTosncs() and connectToNrs() are executing (I have some qDebug inside ) -
@stordd Do you see any of the qDebug outputs you have in your code? Is the connection established?
Post 3 of 11