10-09-2015 01:17 AM
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
10-09-2015 01:36 AM
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
10-09-2015 01:52 AM
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
10-10-2015 11:57 AM - edited 10-10-2015 12:02 PM
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?
10-11-2015 10:32 AM
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
10-11-2015 01:53 PM - edited 10-11-2015 02:01 PM
Yeah, a .NET array of "Empty" size is .... well... a pointer to a memory location of "non-existant"
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!
10-12-2015 04:06 AM
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).
10-12-2015 05:32 AM - edited 10-12-2015 05:37 AM
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.
10-12-2015 05:58 AM
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
10-12-2015 06:33 AM - edited 10-12-2015 06:34 AM
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.