ooth Posted September 15, 2007 Report Share Posted September 15, 2007 If you pass a control's reference to a subVI so you can edit its properties 1) should you close that reference in the subVI? 2) pass that reference back out of the subVI and close it in the main VI (even if you don't need to access it anymore)? 3) don't even worry about closing it because when the main VI exits the reference will be taken care of? I know it's good practice to close references I just don't know where to close it or if I have to? Quote Link to comment
PJM_labview Posted September 15, 2007 Report Share Posted September 15, 2007 Here is my rule of thumb: Whoever open the reference shoud be in charge of closing it. Here is some of the rational behind this: One of the main reason to proceed like this is to improve code clarity. Another reason is to prevent bug. If you close the ref deeper in the hierarchy you might be tempting to branch the source wire upper in the caller to realize later the reference is no longer valid. Therefore, in your example the caller of the SubVI should be in charge of closing it. Just my 2c. PJM Quote Link to comment
TG Posted September 15, 2007 Report Share Posted September 15, 2007 QUOTE(ooth @ Sep 14 2007, 03:54 PM) If you pass a control's reference to a subVI so you can edit its properties1) should you close that reference in the subVI? 2) pass that reference back out of the subVI and close it in the main VI (even if you don't need to access it anymore)? 3) don't even worry about closing it because when the main VI exits the reference will be taken care of? I know it's good practice to close references I just don't know where to close it or if I have to? I guess the main thing is to close it before your APP exits. Where it is done is up to you and depends on how you are using the resource. Closing a ref releases resources(memory) so if memory usage is critical to your app then close it as soon as it is not needed. I would not assume LabVIEW handles the closing for you although it does in some instances such as when the VI that opened the reference goes idle. Quote Link to comment
orko Posted September 15, 2007 Report Share Posted September 15, 2007 QUOTE(PJM_labview @ Sep 14 2007, 09:40 AM) Whoever open the reference shoud be in charge of closing it. I totally agree with PJM. A subVI that has an input of a reference should expect that the reference is already open and valid (and handle it appropriately if it isn't), and should assume that it is not the only subVI that is going to need that reference. Another reason is for modularity and code reuse. For instance, I use a "panelrefs.ctl" cluster in my main VI that is initialized with all of my front panel controls I want to operate on. I can pull these references out at any time in my program from any subVI with the use of an LV2 variable, and close them easily at the end. Having any of my subVI's close the references of the control would cause all subsequent subVI's to not have a valid reference to operate on out of the main panelrefs.ctl. I have all subVI's have a duplicate output control reference, so that if in my data flow I need the reference in the same BD I can tie the calls together without calling it out of the main cluster again. This makes the code a bit cleaner. None of my operator subVI's close the original reference; only the main VI has the responsibility of doing that at shutdown. This makes it easy for me to remember where those references are closed, and prevents bugs popping up that are hard to pinpoint (now where is this silly ref being closed!?!?). Quote Link to comment
crelf Posted September 15, 2007 Report Share Posted September 15, 2007 I agree that you should explicitly close references that you open, but I also agree that it would be nice to know which ones don't need to be explicitly closed... Quote Link to comment
ooth Posted September 15, 2007 Author Report Share Posted September 15, 2007 Thanks for the replies. It's nice to know what the consensus of thinking is on references. Quote Link to comment
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.