Unsolved How to avoid an object of a class to be destroyed when going out of scope in a function
-
@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. -
@jsulm Ok thank you I'm going to try that !
-
@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();
-
@jsulm when i add
TcpServer *server = new TcpServer();
in my
launchApp()
function sometimes my program exit in my slot getSNCS() after doing half of the work and sometimes it exits right away. Any ideas ? -
@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();
-
@jsulm Yes I know but I'm new to programming so I'm trying everything, I know that's not the good way but I have no idea how to solve that.
-
@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?