Callbackcollecteddeligate error in R.NET

Mar 17, 2015 at 3:10 PM
when i debug the code in visual studio it shows the following error
same error id getting from different parts of the code
i will mark that parts
please help me to solve the problem
A callback was made on a garbage collected delegate of type 'R.NET!RDotNet.Internals.blah3::Invoke'. This may cause application crashes, corruption and data loss. When passing delegates to unmanaged code, they must be kept alive by the managed application until it is guaranteed that they will never be called.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;
using RDotNet;
using System.IO;
using WindowsFormsApplication8;


namespace WindowsFormApllication8
{
public partial class Form2 : Form
{
    private readonly RGraphAppHook cbt;
    private readonly RGraphAppHook cbt1;
    private REngine engine;
    public DataFrame dataset;

   // private REngine engine1;
    public static void SetupPath(string Rversion = "R-3.1.2")
    {
        var oldPath = System.Environment.GetEnvironmentVariable("PATH");
        var rPath = System.Environment.Is64BitProcess ?
                               string.Format(@"C:\Program Files\R\R-3.1.2\bin\x64", Rversion) :
                               string.Format(@"C:\Program Files\R\R-3.1.2\bin\i386", Rversion);

        if (!Directory.Exists(rPath))
            throw new DirectoryNotFoundException(
              string.Format(" R.dll not found in : {0}", rPath));
        var newPath = string.Format("{0}{1}{2}", rPath,
                                     System.IO.Path.PathSeparator, oldPath);
        System.Environment.SetEnvironmentVariable("PATH", newPath);
    }
    public Form2()
    {
        InitializeComponent();
        SetupPath();

        REngine.CreateInstance("RDotNet");
        engine = REngine.GetInstanceFromID("RDotNet");
        //engine1 = REngine.GetInstanceFromID("RDotNet");

        cbt = new RGraphAppHook { GraphControl = RPanel1 };
        cbt1 = new RGraphAppHook { GraphControl = RPanel1 };
    }



    private void Form2FormClosed(object sender, FormClosedEventArgs e)
    {
        if (engine != null)
            engine.Close();
    }



    /* private void graphToolStripMenuItem_Click_1(object sender, EventArgs e)
     {
         //inputCSVToolStripMenuItem.Enabled = false;
         //cbt.Install();
         engine.EagerEvaluate("barplot(dataset1$Length)");
         //cbt.Uninstall();
     }*/



    private void inputCSVToolStripMenuItem_Click(object sender, EventArgs e)
    {

        REngine engine = REngine.GetInstanceFromID("RDotNet");

        try
        {
    engine.EagerEvaluate("dataset1<-read.table(file.choose(), header=TRUE, sep = ',')");ERROR HERE
            dataset = engine.EagerEvaluate("dataset1").AsDataFrame();
        }


        catch{
            MessageBox.Show(@"Equation error.");
        }

    }

    private void tableToolStripMenuItem_Click(object sender, EventArgs e)
    {
        Form3 sform = new Form3(dataset);
        sform.Show();
    }


    private void exitToolStripMenuItem_Click(object sender, EventArgs e)
    {
        this.Close();
    }

    private void inSameWindowToolStripMenuItem_Click(object sender, EventArgs e)
    {
        barPlotToolStripMenuItem.Enabled = false;
        cbt.Install();
        engine.EagerEvaluate("plot(dataset1$Length)");ERROR HERE
        cbt.Uninstall();

    }

    private void inANewWindowToolStripMenuItem_Click(object sender, EventArgs e)
    {
        engine.EagerEvaluate("plot(dataset1$Length)");ERROR HERE
    }

    private void inSameWindowToolStripMenuItem1_Click(object sender, EventArgs e)
    {
        ScatterPlotToolStripMenuItem.Enabled = false;
        cbt1.Install();
        engine.EagerEvaluate("barplot(dataset1$Length)");ERROR HERE
        cbt1.Uninstall();
    }

    private void inANewWindowToolStripMenuItem1_Click(object sender, EventArgs e)
    {
        engine.EagerEvaluate("barplot(dataset1$Length)");
    }

   }
}
Developer
Mar 21, 2015 at 10:50 PM
You are visibly using an outdated version of R.NET. Please read the documentation to get the latest version.

You are probably trying to reinitialise the R engine twice in the same process (i.e. closing then reopening a new of you "Form2R"). This is impossible due to a limiration of R itself.