Jump to content

When to close control references?


Recommended Posts

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?

Link to comment

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

Link to comment

QUOTE(ooth @ Sep 14 2007, 03:54 PM)

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?

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.

Link to comment

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!?!?).

Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.