Jan Drozen
Jan Drozen

Reputation: 984

RDotNet memory failures

in my current project I'm using the C# and RDotNet (1.6.5). I am computing some statistics like chi-square (working with frames), correlations and regressions (plots). I want to get some R plots into my app. I am putting the plot output into a PNG file on disk and later I pick it up and reads it via std. IO. Everything looks like it works pretty fine, however sometimes - I am not able to say exactly when - the RDotNet crashes saying something about accessing bad memmory.

It can look like this:

RDotNet error 1

Or like this:

RDotNetError 2

The most common message is:

Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

 at RDotNet.Function.evaluateCall(IntPtr call)
 at RDotNet.Function.createCallAndEvaluate(IntPtr argument)
 at RDotNet.Function.InvokeNamedFast(Tuple`2[] args)
 at RDotNet.REngineExtension.CreateDataFrame(REngine engine, IEnumerable[] columns, String[] columnNames, String[] rowNames, Boolean checkRows, Boolean checkNames, Boolean stringsAsFactors)

Note that the CreateDataFrame can be not the only place where the error appears. Once it appears just here, once when calling lm function, once while evaluating some different formula.

Current test scenario was (simply said): Compute A, B, A, B, A, fail. I want to say it crashes while doing something that has been correctly evaluated yet.

Can anybody please provide some workaround? The worst thing about this is that RDotNet is a singleton. It is not possible to simply catch the error, dispose REngine and create new one.

Upvotes: 2

Views: 653

Answers (1)

Jan Drozen
Jan Drozen

Reputation: 984

For future googlers:

After some research I came to the idea the errors are caused by accessing the R environment from different threads. Not at the same time, but still from different threads (in my case processing clients requests).

My solution is creating separate singleton thread for the environment and synchronize access to it using a facade. No more errors occurs after this "hack" application.

Upvotes: 2

Related Questions