forum.alglib.net
http://forum.alglib.net/

Access violation in C# COM dll
http://forum.alglib.net/viewtopic.php?f=2&t=342
Page 1 of 1

Author:  RvanStapele [ Fri Apr 15, 2011 1:05 pm ]
Post subject:  Access violation in C# COM dll

I am testing an algorithm using the AlgLib library. The algorithm must be used by both older Win32 programs and .net programs.

To realize this the algorithm is placed in a .net assembly dll that can be used by .net programs directly. To interface to older win32 programs I created a .net COM dll which is then called from a Win32 dll or program:

e.g.: Win32 exe -> Win32 DLL -> .net COM dll -> .net assembly dll

I tested the algorithm directly with a .net program and this works fine. Calling from a Win32 program generates an access violation in the AlgLib library. However, calling other functions of the algorithm that do not use AlgLib show no problems, which means that the chain of dll calls is ok.

The access violations usually occur in alglib.spline1d.spline1dcalc using akima splines trough 100+ points.

Because c# is managed code I expected that the exceptions would be trapped by a try catch block, but this doesn't happen, the application simply crashes!

Does anyone have any idea what goes wrong? Could it be a memory problem through the com interface?

Author:  Sergey.Bochkanov [ Sat Apr 16, 2011 6:48 am ]
Post subject:  Re: Access violation in C# COM dll

Can you post your code? It is hard to tell what's going wrong without actually looking at the code.

I think that there exists a bug in your code, which works with ALGLIB, not in ALGLIB itself. I don't want to tell that ALGLIB is perfect :) but it works when called from C#, hence it must work when called from native app.

Author:  RvanStapele [ Sun Apr 17, 2011 1:11 pm ]
Post subject:  Re: Access violation in C# COM dll

Hello Sergey,

Here is a sample of the code I use. Nothing special really. I debugged it over and over and as long as it is called from a .net program there is no problem. When this is called from a COM interface I get an access violation although the call is made from an try catch block. This shouldn't be possible.

Actually I don't think that there is a bug in your or my code but that this code triggers a problem in the .net framework in combination with COM interop.

I guess there is some memory management problem. e.g. During debugging the com interface, I changed the code from:

vector.Add(alglib.spline1dcalc(splineInts[col], newIndices[row])); //Vector = List<double>

to:

double x = alglib.spline1dcalc(splineInts[col], newIndices[row]);
vector.Add(x);

which is a useless change, but I could run the calculation without a problem. Restarting the application with this code resulted in an access violation again.

Do you have an idea what could be the cause of the problem? Do you use recursion in the spline calculations? Can the stack size be a problem?
Maybe it is the Vector.Add function that crashes the memory manager when called 700 times?

My code:
//build splines for all slaves based on rows
alglib.spline1dinterpolant[] splineInts = new alglib.spline1dinterpolant[nSlaves];
for (int i = 0; i < nSlaves; i++)
{
double[] sval = absMatrix.ValuesBySlaveIndex(i); //get an array with y-values
alglib.spline1dbuildakima(absIndices , sval , out splineInts[i]); //absIndices=x-values, sval=y-values
}

//interpolate slave splines based on newRows
PreMatrix newMatrix = new PreMatrix(); //Matrix with slave positions in a column
for (int row = 0; row < newIndices.Count; row++) //NewIndices = array with new x-values
{
PreVector vector = new PreVector();
for (int col = 0; col < nSlaves; col++)
{
vector.Add(alglib.spline1dcalc(splineInts[col], newIndices[row]));
}
newMatrix.Add(vector);
}

Author:  Sergey.Bochkanov [ Mon Apr 18, 2011 8:30 am ]
Post subject:  Re: Access violation in C# COM dll

Your C# code seems to be right, at least those parts which work with ALGLIB.

As for ALGLIB, it uses memory or stack very carefully, without unneeded allocations or recursions. In particular, spline needs about 16*N bytes. In C# you should be able to call spline construction/calculation functions as long as you have enough memory. In C++ things are a bit different, your memory can become fragmented, but C# do not have memory fragmentation problems.

Unfortunately, I don't know what to recommend you in your situation.

Page 1 of 1 All times are UTC
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/