.Call using R.Net

May 7, 2013 at 11:22 AM
Hi group,

I am using R.Net to load a dll file which I have created for a C code compiled using R CMD SHLIB. It has a function which I can acess in R using the .Call() method.
But when i run the code using R.Net i get AccessViolationException as follows,

System.AccessViolationException was unhandled
Message=Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
   at RDotNet.Internals.DirectNativeMethods._R_tryEval(IntPtr statement, IntPtr environment, Boolean& errorOccurred)
   at RDotNet.Internals.DirectNativeMethods.R_tryEval(IntPtr statement, IntPtr environment, Boolean& errorOccurred)
   at RDotNet.Expression.TryEvaluate(Environment environment, SymbolicExpression& result)
   at RDotNet.REngine.Parse(String statement, StringBuilder incompleteStatement)
   at RDotNet.REngine.<Evaluate>d__0.MoveNext()
   at System.Linq.Enumerable.LastOrDefault[TSource](IEnumerable`1 source)
   at RDotNet.REngine.EagerEvaluate(String statement)
   at RdotNet.Program.Main(String[] args) in c:\users\user.name\documents\visual studio 2010\Projects\RdotNet\RdotNet\Program.cs:line 41
   at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()
But when i run the same DLL in R it works fine without any problem.
So i just want to know is .Call() supported in R.Net, if yes ,can anyone please give a simple example for this.
May 24, 2013 at 11:58 PM

Thanks for providing the stack trace, it is useful to provide conditions. It seems from it that you are running your program from visual studio; do you observed the same access violation crash if run from the command prompt? if not, this may be a threading issue when run from visual studio, though a surprise.

It sounds from your post that you can indeed run your '.C' call from R successfully: can you confirm?

Access Violation crashes, from what I experience, occur typically when the C code access a class/struct element via a pointer that is NULL, or accessing a pointer that points to freed memory. While it could in principle be because R or .NET garbage collects something you passed to your .C function, both R and R.NET are designed not to let that happen in your context, so far as I can see.

One thing you could do to diagnose further is to compile your C code with debug symbols and debug your program specifying that both the native and managed code should be debugged. Probably the behavior when launched from visual studio, and if you attach VS to a running process (Ctrl Alt P) you can specify what managed/unmanaged code you want debugged.

Hope this helps