LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Should I close the reference?

Solved!
Go to solution

Hi

 

I'd like to ask... should I close the reference of the Workbook in the following code?

 

20160112001.png

 

In fact, I have little idea whether to close a reference or not, since I thought LabVIEW will automatically close all the references after the execution.

I also found someone said the references of activeX should be closed on my own.

Could someone correct my understanding of closing reference?

0 Kudos
Message 1 of 10
(3,834 Views)
When in doubt, close references. If it wasn't needed, no harm done. I tend towards closing references rather than trying to keep track of what references I need to close when I'm finished, and what ones I don't. Besides the rules have been known to change.

Mike...

Certified Professional Instructor
Certified LabVIEW Architect
LabVIEW Champion

"... after all, He's not a tame lion..."

For help with grief and grieving.
Message 2 of 10
(3,811 Views)
Solution
Accepted by William1225

In this case I'm pretty certain the cleanup VIs will close those references so I don't think you need to close them your self.  One way I usually test this is I put the code in a while loop, or use the continuous run button.  This is basically the only time I ever use it and I only do on small subVIs like this.  I then run my code over and over again and look at the task manager to see how much memory LabVIEW.exe is using.  After a few seconds if there is a memory leak then memory will continue to increase.  If memory goes up and down a little, but overall doesn't continue to grow, then all references that need to be closed, are being closed.

 

There is an NI white paper on closing VI Server references which is exactly what you are doing but has some relevant information.

 

http://www.ni.com/white-paper/14393/en

 

And some discussion on it.

 

https://lavag.org/topic/16443-closing-references-in-labview-new-documentation-published/

Message 3 of 10
(3,791 Views)
Solution
Accepted by William1225

The answer is -- NO, do not close the Reference.  Why?  You should not have a reference!!  Do not use ActiveX if you don't have to -- there's a perfectly good "Save Report" function that will handle the Save for you, and handle the references as well.

 

Simple Excel.png

This differs from your example in several ways.

  1. It uses Save Report instead of ActiveX "Save Workbook" to write the file to Excel, naming it as it does so.
  2. It wires "True" to the Dispose Report to close Excel when it is done (leaving it False will leave the Workbook open).
  3. It does not wire the file to the Report Open function -- you should only wire something there if the file already exists and you are going to modify it (which is a perfectly reasonable thing to do, but for this Demo, where I didn't have a test.xlsx already created, I would get a Run Time error).

Notice that I did not close the reference, as advertised ...

 

Bob Schor

 

Message 4 of 10
(3,739 Views)
Solution
Accepted by William1225

William1225 a écrit :

Hi

 

I'd like to ask... should I close the reference of the Workbook in the following code?

  

In fact, I have little idea whether to close a reference or not, since I thought LabVIEW will automatically close all the references after the execution.

I also found someone said the references of activeX should be closed on my own.

Could someone correct my understanding of closing reference?


I agree with Bob that in this case you shouldn't even use the GetActiveXReferences.vi.

 

In my opininion you should NEVER close the references you get from the Get ActiveX References.vi and let the Dispose Report.vi close these references. If you open references to other objects then you should close these ones (e.g. a reference to a range on a worksheet) when they are no longer needed.

 

As an exemple, if you close the sheets reference you got from the Get ActiveX References.vi and then use a RGT vi that uses this reference you will get an invalid reference error.

 

Ben64

 

In the following exemple the _Worksheet reference is not closed but the other ones are.

 

Close Ref Exemple.png

Message 5 of 10
(3,719 Views)

Hi mikeporter,

 

Very thanks to your suggestion:)

Your suggestions are always helpful.

Closing references saves much effort without tracing which reference should be closed.

However, it appears a little redundant if it is not necessary Smiley LOL

0 Kudos
Message 6 of 10
(3,671 Views)

Hi Hooovahh,

 

Very thanks to your reply.

The links are helpful, I should read it earlier Smiley Very Happy

0 Kudos
Message 7 of 10
(3,665 Views)

Hi Bob_Schor,

 

You're totally right, I can't believe I didn't see the Save Report.vi...Smiley Mad

I'd originally like to keep the report open and save the data in the file, but when I wire a false constant with the "close report?" and a true constant with the "save change?" in the Dispose Report.vi, it failed and the Help said "The VI ignores this input(save changes?) if the Close Report input is FALSE."

Then I'd realized I have to add the save function before the Dispose Report.vi.

And your suggestion about wire the path in the Save Report.vi is a great idea, I'd never thought of it.

Very thanks to your suggestions!!!

0 Kudos
Message 8 of 10
(3,656 Views)

Hi ben64,

 

Your explanation is quite clear.

I feel all the replies of this post are very helpful to correct my understanding in different aspects Smiley Very Happy

I hope it is okay that I've marked many "Accept as a solution" Smiley LOL

Thank you very much!!!

0 Kudos
Message 9 of 10
(3,643 Views)

@William1225 wrote:

 

I hope it is okay that I've marked many "Accept as a solution" Smiley LOL


It's fine as long as there really are multiple solutions.  Here is a quote from the unofficial guidelines.

 

  • Threads can have multiple posts marked as a solution, but it is best to limit the number of posts that are the solution.  Mark the posts that answer the question, not just helpful posts.  A helpful post can be Kudo'd, but doesn't need to be marked as a solution.
0 Kudos
Message 10 of 10
(3,627 Views)