ni.com is currently undergoing scheduled maintenance.

Some services may be unavailable at this time. Please contact us for help or try again later.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Should I close a TCP Open Connection reference that errors?

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:

cbutcher_0-1601970121061.png

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...

 


GCentral
0 Kudos
Message 1 of 4
(2,289 Views)

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.

Message 2 of 4
(2,255 Views)

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.

0 Kudos
Message 3 of 4
(2,250 Views)

I'd prefer a state pattern 😁.

0 Kudos
Message 4 of 4
(2,245 Views)