forum.alglib.net

ALGLIB forum
It is currently Sat Dec 21, 2024 2:41 pm

All times are UTC


Forum rules


1. This forum can be used for discussion of both ALGLIB-related and general numerical analysis questions
2. This forum is English-only - postings in other languages will be removed.



Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: Access violation in C# COM dll
PostPosted: Fri Apr 15, 2011 1:05 pm 
Offline

Joined: Fri Apr 15, 2011 12:42 pm
Posts: 2
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?


Top
 Profile  
 
 Post subject: Re: Access violation in C# COM dll
PostPosted: Sat Apr 16, 2011 6:48 am 
Offline
Site Admin

Joined: Fri May 07, 2010 7:06 am
Posts: 927
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.


Top
 Profile  
 
 Post subject: Re: Access violation in C# COM dll
PostPosted: Sun Apr 17, 2011 1:11 pm 
Offline

Joined: Fri Apr 15, 2011 12:42 pm
Posts: 2
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);
}


Top
 Profile  
 
 Post subject: Re: Access violation in C# COM dll
PostPosted: Mon Apr 18, 2011 8:30 am 
Offline
Site Admin

Joined: Fri May 07, 2010 7:06 am
Posts: 927
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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 7 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group