NET.addAssembly given arithmetic overflow

I am using a toolbox that makes use of a NET.addAssembly (developed by DHI namely to read in result files of their software). I am however not familiar with this NET.addAssembly programming and when I adjusted one of their files, I am getting the following error message when I try to read in the result file:
MATLAB:NET:CLRException:PropertyGet
Message: Arithmetic operation resulted in an overflow.
What can be causing this? The line that is called cannot lead to an overflow as it only contains 123 doubles as a result. I have also been careful to always close the file that I have opened with the close function provided (through an OnClean-up function).
First the problem only occurred after several calls to my function were made in the same matlab-session and it helped to close matlab and open again. But now I get this error message every time, even after restarting my computer!, so I cannot run my function any more.
What should I look for in my code?

13 Comments

okay, by doing some more research I have found out that the property I was trying to read in does not exist anymore (where it did before because I made a list of it to know all the properties and it also was working before)
however, when I check the files of the toolbox, none of the files are modified according to their dates... How can I have made changes to the properties then?
Can you show the portion of code that causes the error and which particular line results in the error?
What did DHI say when you asked them about their software?
They answered that the matlab toolbox is an interface for which they do not provide support. This is also indicated on their website so that is why I try it here.
I have found out that the problem is only occurring when I first rund a mex-file that I have generated myself. If I run the function that uses the Net.addAssembly, it works fine, up to the point that I run my whole program where it is incorporated after a mex-file. I have checked my mex-code for memory leaks but it has none, I double checked with the task manager.
Is there an incompatibility of mex with the Net.addAssembly method?
Ingrid
Ingrid on 4 Sep 2014
Edited: Ingrid on 4 Sep 2014
Nobody has any idea?
How can I check what is happening with the Net object creation? The toolbox is working fine as long as no mex is involved. As soon as I use the SDK compiler to create a mex-file the problem occurs i.e. one of the properties is not created any more. Since this occurs even when I am not actually running the mex-file (so already afer just compiling) I do not think the problem lies within my c-code but it is more general linked to an incompatibility of c-code with the Net Add assembly method and I would like to have some help to sort this out since I need both and I have no idea on how to proceed now
some additional information: I am using R2014a and the SDK 7.1 compiler. On my previous computer on which R2012a was installed there was no such problem so it seems to be a version issue. Can anyone check this?
Guillaume
Guillaume on 4 Sep 2014
Edited: Guillaume on 4 Sep 2014
Without you showing any bit of code, it's difficult to help you. Maybe show us the bit of code where the error occurs and since it seem to matters the source of the mex file.
Have a look at what properties(whicheverobjectiscausingtheerror) says when the error occurs. Maybe it'll give a clue.
NET.addAssembly('DHI.Generic.MikeZero.DFS');
import DHI.Generic.MikeZero.DFS.*;
res11File = DfsFileFactory.DfsGenericOpen(infile);
for ii = 1:res11File.ItemInfo.Count
itemInfo = res11File.ItemInfo.Item(ii-1);
coords = itemInfo.SpatialAxis.Coordinates;
...
end
this last line is causing the error namely:
MATLAB:NET:CLRException:PropertyGet
Message: Arithmetic operation resulted in an overflow.
Source: DHI.Generic.MikeZero.DFS
when I ask for properties(itemInfo) it gives a long list, including the Spatial Axis i.e. see below for shortened list
Properties for class DHI.Generic.MikeZero.DFS.DfsDynamicItemInfoWrapper:
AssociatedStaticItemNumbers
ValueType
SpatialAxis
MaxValue
...
However when I hover over the ItemInfo in debug mode, the SpatialAxis property does not show up in the list. For some reason it is not loaded but as mentioned before this does not always occur. When it is occuring and when not is not completly clear to me yet. It seems to be linked to using the mex compiler as most of the times the error occurs after compilation of a c-code but not always so maybe there is something else that is causing the behaviour which I am overlooking. However, when stepping through the code, the first time the SpatialAxis property is almost always loaded and the error occurs in the second or later step (if later, than always occurs sooner each time the debugging process is repeated)
To show the problem more clear:
properties(res11File.ItemInfo.Item(4))
gives as output:
Properties for class DHI.Generic.MikeZero.DFS.DfsDynamicItemInfoWrapper:
AssociatedStaticItemNumbers
ValueType
SpatialAxis
MaxValue
MinValue
ItemNumber
Name
Quantity
DataType
ReferenceCoordinateX
ReferenceCoordinateY
ReferenceCoordinateZ
OrientationAlpha
OrientationPhi
OrientationTheta
ConversionType
ConversionUnit
AxisConversionType
AxisConversionUnit
ElementCount
UsedElementCount
Bytes
UsedBytes
res11File.ItemInfo.Item(4)
gives as output:
ans =
DfsDynamicItemInfoWrapper with properties:
AssociatedStaticItemNumbers: [1x1 System.Collections.ObjectModel.ReadOnlyCollection<System*Int32>]
ValueType: [1x1 DHI.Generic.MikeZero.DFS.DataValueType]
MaxValue: 25.7016
MinValue: 24.5110
ItemNumber: 5
Name: [1x1 System.String]
Quantity: [1x1 DHI.Generic.MikeZero.eumQuantity]
DataType: [1x1 DHI.Generic.MikeZero.DFS.DfsSimpleType]
ReferenceCoordinateX: -1.0000e-35
ReferenceCoordinateY: -1.0000e-35
ReferenceCoordinateZ: -1.0000e-35
OrientationAlpha: -1.0000e-35
OrientationPhi: -1.0000e-35
OrientationTheta: -1.0000e-35
ConversionType: [1x1 DHI.Generic.MikeZero.DFS.UnitConversionType]
ConversionUnit: [1x1 DHI.Generic.MikeZero.eumUnit]
AxisConversionType: [1x1 DHI.Generic.MikeZero.DFS.UnitConversionType]
AxisConversionUnit: [1x1 DHI.Generic.MikeZero.eumUnit]
ElementCount: 15
UsedElementCount: 15
Bytes: 60
UsedBytes: 60
which is of course causing a problem when calling
res11File.ItemInfo.Item(4).SpatialAxis
because it is a non-existing property even though the property is defined in the class
Can you show the output of
superclasses(res11File.ItemInfo.Item(4))
and
methods(res11File.ItemInfo.Item(4))
From the name it's possible that it derives from System.Dynamic.DynamicObject and implements dynamic properties that for some reason are not always available.
superclasses(res11File.ItemInfo.Item(ii-1))
Superclasses for class DHI.Generic.MikeZero.DFS.DfsDynamicItemInfoWrapper:
DHI.Generic.MikeZero.DFS.DfsAbstractItemInfoWrapper
System.Object
handle
DHI.Generic.MikeZero.DFS.IDfsDynamicItemInfo
DHI.Generic.MikeZero.DFS.IDfsItemInfo
DHI.Generic.MikeZero.DFS.IDfsSimpleItemInfo
DHI.Generic.MikeZero.DFS.IDfsSimpleDynamicItemInfo
methods(res11File.ItemInfo.Item(ii-1))
Methods for class DHI.Generic.MikeZero.DFS.DfsDynamicItemInfoWrapper:
Equals SetAxisUnitConversion SetUnitConversion
GetHashCode SetOrientation ToString
GetType SetReferenceCoordinates
thank you very much for your help as this is really going beyond my programming knowledge. It is strange that sometimes it is available and other times not as it is the same file that is being read in each time (just for testing purpose now) and it depends on the previous commands in matlab
From all the properties of this class, the SpatialAxis property is the only one that has more properties hiden under it i.e. all the others contain only one value whereas the SpatialAxis contains more properties such as
res11File.ItemInfo.Item(0).SpatialAxis
ans =
DfsAxisNeqD1 with properties:
AxisType: [1x1 DHI.Generic.MikeZero.DFS.SpaceAxisType]
Dimension: 1
SizeOfData: 42
Coordinates: [1x1 DHI.Generic.MikeZero.DFS.Coords[]]
AxisUnit: [1x1 DHI.Generic.MikeZero.eumUnit]
so this is a different kind of property than the others which might be part of the solution
You say that sometimes it is available and other times not as it is the same file that is being read in each time, but from your example it would appear that the property is always available for res11File.ItemInfo.Item(0) and never available for res11File.ItemInfo.Item(4) (for that particular file).
Can you confirm that's what you're seeing, or is it sometimes available for the same item and sometimes not?
it is always available for res11File.ItemInfo.Item(0) but after that the number for which it is not available varies. Sometimes already for res11File.ItemInfo.Item(1), last time it was for res11File.ItemInfo.Item(4), but sometimes it goes all the way to res11File.ItemInfo.Item(25) which is the maximum for this file and no error occurs at all. This is however only when I have not compiled any mex-code
the reason I think the mex-compiling has something to do with it is because first the function works completely and then after a first compilation it worked up to 4 and after a second compilation it worked up to 1
the code for the mex compilation is just
mex -g modelName.c
so should not cause any problems

Sign in to comment.

 Accepted Answer

From the name of the class, it looks like DHI have implemented some sort of dynamic object with dynamic properties. I've no idea how they've done it since your object does not even derive from DynamicObject.
In any case, since you don't have any control over the object behaviour, I think the best you can do is check if the property exists before calling it:
for ii = 1:res11File.ItemInfo.Count
itemInfo = res11File.ItemInfo.Item(ii-1);
if any(strcmp(properties(itemInfo), 'SpatialAxis'))
coords = itemInfo.SpatialAxis.Coordinates;
%do something with coords
else
%sorry coords not available. make do without (use a default?)
end
end
Or you could wrap your call in a try catch:
for ii = 1:res11File.ItemInfo.Count
itemInfo = res11File.ItemInfo.Item(ii-1);
try
coords = itemInfo.SpatialAxis.Coordinates;
catch err
if strcmp(err.identifier,'MATLAB:NET:CLRException:PropertyGet')
coords = somedefaultvalue
else
rethrow(err); %some other error. Better tell you about it.
end
%do something with coords
end

5 Comments

Ingrid
Ingrid on 4 Sep 2014
Edited: Ingrid on 4 Sep 2014
I already implemented the try - catch method so that not everything crashes, but unfortunatly I really need this value and cannot use a default for it so that is why I was trying to see if anyone has an idea where I can search
Is there a way to debug the object behaviour or something like that so that I can step through the code that is being called one by one or is this impossible (is it somehow compiled as I cannot seem to find the source anywhere)
Ingrid
Ingrid on 4 Sep 2014
Edited: Ingrid on 4 Sep 2014
I think this expains a bit how they created the dynamic object even though I really have no idea what they are talking about
There are tools to decompile .Net code, I don't think any of them are free. Once decompiled you could try to debug it in visual studio but if you don't know .Net, you probably won't get far.
The behaviour your seeing is extremely odd, I don't see how the mere act of compiling can affect .Net code. I would say the only thing you can do is not compile the mex file if you're going to be using DHI's library (or wait until you've read all the files).
Otherwise, if you have the details of the file format, write your parser.
Ingrid
Ingrid on 5 Sep 2014
Edited: Ingrid on 5 Sep 2014
OK, thank you very much for you answer. I will try to re-organize my code to take this into account and issue a warning to the user when it does happen as it will be part of a larger program. Hopefully it will be resolved in a next version or someone else will have an idea on how to proceed
I didn't see the first time round the short extract of the manual that you've posted. it's not really anything to do with the issue you're having, it only talks about another method (COM) of communicating with their code.
However, you can also use COM from matlab. As your document says it's a bit more awkward than the .Net interface but maybe it'd work better.
You would have to find the ProgId of the COM interface, it should be somewhere in the documentation. Then you would access your file with something like:
dfsreader = actxserver('DFSProgId.DfsFileFactory'); %possibly
dfshelper = actxserver('DFSProgId.DfsComHelper'); possibly
res11File = dfsreader.DfsGenericOpen(infile);
%as explained you need to use the DfsComHelper to access iteminfo
iteminfos = dfshelper.GetItemInfoDfs(res11File);
for ii = 1:iteminfos.Count
iteminfo = iteminfos.Item(ii-1);
coords = iteminfo.SpatialAxis.Coordinates; %possibly
%...
end
%at the end, release the COM interfaces
delete(dfshelper);
delete(dfsreader);
It might be a bit more complicated if any object you need implements several interfaces as you may need to switch interface to access the right method (as explained in the 2nd part of your document), which you would do using invoke. For example, according to that doc, IDfsFile has two more interfaces
interfaces(res11File)
%should return {'ProgId.IDfsFileIO', 'ProgId.IDfsFileStaticIO'}
%use a different interface:
res11FileAsFileIO = invoke(res11File, 'IDfsFileIO');
res11FileAsFileIO.methodofFileIO(...);

Sign in to comment.

More Answers (1)

xu chen
xu chen on 26 Nov 2019
I had the same problem as you. Excuse me, how did you solve it in the end? Thank you very much!

Asked:

on 29 Aug 2014

Answered:

on 26 Nov 2019

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!