10-06-2020 02:46 AM
Each time I open a TCP connection using "TCP Open Connection", of course I should close the connection when I'm finished.
I have some code that appears to be working fine, but now to handle an additional condition I want to put the Init code in a loop (so that it keeps trying until it succeeds, when the other end of the connection is not initially available).
If my code basically boils down to something like this:
then do I have a memory leak in the first loop? Should I be explicitly closing the (failed) connection in the error case (i.e. every iteration except the last?)
I'm a little concerned about "half-open(?)" TCP connections, although I don't know how/if LabVIEW interacts with these...
10-06-2020 05:49 AM
What do you mean by "half open"?
If the connection fails to open, the reference is 0 (not a reference). Then it doesn't need closing. The only way to get a reference, AFAIK, is when the connection open succeeds.
I think you should close on error, because you could get a valid connection, and a time out on the write or read. At the moment, that won't close, and will cause another problem, as the connection stays open. So a new connection might fail if the server only allows one connection.
10-06-2020 06:30 AM
I agree with wiebe@CARYA on the answer. I did notice that this sounds like a great opportunity for a state machine architecture. Perhaps you are already using a state machine in your actual code, but I just thought that I would suggest it in case. No need for duplicate code.
10-06-2020 06:35 AM
I'd prefer a state pattern 😁.