LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Converting Empty Array to .NET object

Hi All,

 

When converting an empty array to .NET object I get "Error 1172 occurred at Invoke Node in To .NET Object.vi->Issue in Converting an Empty Array to .NET obj.vi". But it is not the case always, when I give some value in the array(make it non empty) and delete it again(make it empty agian) I am not getting this error. Can some one give me explanation on this behaviour.

 

PS: Find the VI and image attached

 

Thanks in Advance,

Sree

 

0 Kudos
Message 1 of 12
(6,538 Views)

Forgot to mention few details

 

I am using LabVIEW 14.0 and deleting array using delete element, for "Empty Array" I am still getting error as expected

 

Thanks in Advance,

Sree

0 Kudos
Message 2 of 12
(6,519 Views)

Hi All,

 

I am also facing the same issue when using a Boolean array. I tried using a String array instead, and followed the steps as mentioned; but in this case I get an error 1172 whenever the array is empty, irrespective of whether I have deleted the elements in it or I have not added any elements to it.

 

Any insights on it are welcome.

 

Regards,

S.P.Prasaanth

0 Kudos
Message 3 of 12
(6,507 Views)

Wow! I also just ran across this issue in my code.  If i try to convert an empty array (in my case, an array of double) to a .Net object, I get the 1172 error.

 

Is this something that previously worked, or did we all just coincidentally stumble across this same issue in the same 24-hour period?

 

 

0 Kudos
Message 4 of 12
(6,460 Views)

I'm not that familiar with .NET, but when I went on the Web to look up .NET array objects,  I found "The number of dimensions and the length of each dimension are established when the array instance is created. These values can't be changed during the lifetime of the instance."  This suggests that an empty Array might not be a valid .NET Object, hence returning Error 1172 ("A .NET exception occurred in an external assembly.") might be appropriate.

 

Bob Schor

Message 5 of 12
(6,429 Views)

Yeah, a .NET array of "Empty" size is .... well... a pointer to a memory location of "non-existant" Smiley Wink In fact, you need no memory to store an array of <insert datatype here> with length = 0 so, why would you return anything other than an error when asking to allocate Zero memory?

 

It sounds a lot like giving a squirrel zero nuts, asking the squirrel to bury those nuts and then asking the critter to remember where it did not bury the nuts you did not give it!


"Should be" isn't "Is" -Jay
0 Kudos
Message 6 of 12
(6,413 Views)

My guess would be that this is simply a bug in whichever code LV uses to create the .NET object (for instance, if it tries to iterate over the array elements when getting the type).

 

The obvious workaround is to see if there's an empty array and in that case generate the value yourself (either by creating an array and emptying it or by using whichever .NET code is available for that). Personally, I don't think I ever used this VI, as when interacting with .NET, I found that you either get clean conversions or it's all .NET references, but my experience with .NET is fairly limited.

 

Bob, even if arrays in .NET are immutable (which would surprise me. I would expect .NET to have a mutable type), that isn't a good reason for not allowing an empty array. I would be surprised if it wasn't possible (and the demonstration that this conversion can work under certain circumstances suggests that's not a problem).

 

Jeff, same thing. My understanding of .NET memory management (and keep in mind I'm not a .NET programmer) is that it's much more similar to LV than to C. I see no reason in principle why you wouldn't be able to create an array with 0 elements (which you can also easily do in C).


___________________
Try to take over the world!
0 Kudos
Message 7 of 12
(6,374 Views)

Arrays itself are not really immutable but not resizable just as in Java. If you need resizable arrays you have to use object types derived from the List interface or something similar.

 

Problem with conversions like this is that there is not a single target type but really many different possible types. .Net Arrays is the most direct corresponding type but being non resizable a array of 0 elements makes no sense.

Rolf Kalbermatter  My Blog
DEMO, Electronic and Mechanical Support department, room 36.LB00.390
0 Kudos
Message 8 of 12
(6,360 Views)

Thank You All,

 

But I can see that C# supports empty array(though it has be reintialized when needed) in http://stackoverflow.com/questions/8727146/how-to-initialize-empty-array-in-c-sharp.

 

Regards,

Sree

0 Kudos
Message 9 of 12
(6,344 Views)

Just as an extra tidbit I think I know why there is this difference. LabVIEW knows internally of two types of empty arrays and strings. One is simply a NULL handle and the other is a handle with the count member set to 0.

 

The reason for this is optimization. An array of 1'000'000 empty strings consumes about 4 MB of memory when using NULL for the empty strings and about at least 12MB of memory for when using explicit empty handles (in reality even more since every handle also consumes some extra bookkeeping overhead of about 8 to 16 bytes per handle).

 

Initially when the VI is loaded the empty array in the control is initialized to a NULL handle internally. When you edit the control and add an element a real handle is allocated to strore this element. When you then delete that element the handle is not really changed other than updating the count parameter to indicate that 0 elements are there. That is a trivial operation with virtually zero cost while physically deallocating the handle is pretty expensive as the memory manager is getting invoked and potentially that could cause a whole bunch of extra operations too.

 

Now almost everywhere in LabVIEW a NULL handle is simply considered an empty array (or string) but this function seems to make a difference. This could be an oversight but it is even more likely that this is actually intended in the function that is getting used here, as .Net doesn't have the same behaviour of considereing a NULL object automatically as an empty object. I agree that in this specific case it could be considered as equal but in many other cases where the function that is used in this VI may get used, this is likely not the case.

 

Rolf Kalbermatter  My Blog
DEMO, Electronic and Mechanical Support department, room 36.LB00.390
0 Kudos
Message 10 of 12
(6,335 Views)