LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

save .NET object in global variable

Hi,
 
I wrote an application in LabVIEW8 where I create a .NET object with a .NET constructor node and put the reference into a shared variable. I am able to use at any time in any VI this shared variable to get the reference again and access my object.
 
But now I change from shared variable to "standard" global variable in Labview (because I dislike all the .vi it create each time a create a new shared variable). And now, when I whant to access my .NET object, I get the error:
Error 1 occurred at Invoke Node in Untitled 1.vi
Possible reason(s):
LabVIEW:  An input parameter is invalid.
---
NI-488:  Command requires GPIB Controller to be Controller in Charge.
 
But the global variable contains the reference number of my object. It seems to be, that the global variable keep the reference of my object but the object itself is closed after I quit the VI where I created it. It is true? What can I do again this?
 
Cheers,
Risotto
0 Kudos
Message 1 of 4
(3,486 Views)
You may want to post your code, or an example that replicates your issue so that we have a better understanding of what and why this is happening.

I would also discourage the use of Global Variables... but that is a completely different discussion.
0 Kudos
Message 2 of 4
(3,484 Views)

Hi Matt,

I sent you an example of what I mean exactly.

To reproduce the problem, please open the VIs "CreateFile.vi" and "WriteToFile.vi". Then execute the VI "CreateFile.vi" at first to create the .NET object and put the reference in the global variable and then execute the VO "WriteToFile.vi". You should get the error number "1".

As proove that my dll is working, I put a running example "RunningExample.vi" where I am not using a global variable.

Regards,
Risotto

 

By the way, I read a lot of time in the forum that using global variable is not the best. But I know no alternative how to save variables that I can reach from any other VI.

 

0 Kudos
Message 3 of 4
(3,479 Views)
As MattH said, global variables aren't a great idea in any LV program, but I digress...
 
The rules for a .NET refnum are as follows. I'm not sure whether it's #1 or #3 that might be biting you, but let me know if this helps.
 
1. The refnum is only valid within the project context in which it is created.
 
If you aren't using projects, then you can consider the that as a default project for the sake of the refnum. So you can't pass them around from one project or machine to another.
 
2. The refnum must be closed by the user application.
 
Once you have a .NET refnum, you must close it or you'll get a memory leak in your application. I wrote a blog entry to explain how this works, and common cases that people don't think about.
 
3. Any refnums you don't close are closed by LV when the program stops.
 
Okay, this isn't in conflict with #2, because it only happens when the program stops. If you have a long running program, #2 above is a very serious issue to remember.
 
However, when you're in the IDE, what constitues a program stop? Well, again we go back to the project context. When all VI's in the project (or the non-project case) stop executing, LV is going to close out any open references.
0 Kudos
Message 4 of 4
(3,478 Views)