CallbackOnCollectedDelegate was detected

Jun 25, 2012 at 10:59 AM
Edited Jun 26, 2012 at 2:19 AM

I use this code to read .csv data.

static void Main(string[] args)
        {
            REngine.SetDllDirectory(RHelper.GetRPath());
            REngine.CreateInstance("RDotNet", new[] { "-q" });
            REngine engine = REngine.GetInstanceFromID("RDotNet");
            string filePath = System.Environment.CurrentDirectory + "\\data\\data.csv";
            string input = "";
            using (StreamReader sr = new StreamReader(filePath))
            {
                string line = "";
                while ((line = sr.ReadLine()) != null)
                {
                    input += line + @"\r\n";
                }
            }
            string dataFrameExpression = "tb";
            DataFrame tb = engine.EagerEvaluate(dataFrameExpression + "<-read.table(text='" + input + "',sep=',',header=TRUE,as.is=TRUE,na.strings = '')").AsDataFrame();
            var result = engine.EagerEvaluate("lm.employee<-lm(salary~salbegin+jobtime+educ+jobcat,data=tb)").AsList();
            var summary = engine.EagerEvaluate("summary(lm.employee)");
            var confficients = engine.EagerEvaluate("lm.employee$coefficients").AsNumeric();
            Console.WriteLine(string.Join("\t", confficients.Names));
            Console.WriteLine(string.Join(" ", confficients));
            engine.Close();
            Console.Read();
        }

My csv data file like this:

 

id,gender,bdate,educ,jobcat,salary,salbegin,jobtime,prevexp,minority,age
1,m,2/3/1952,15,3,57000,27000,98,144,0,52
2,m,5/23/1958,16,1,40200,18750,98,36,0,46
3,f,7/26/1929,12,1,21450,12000,98,381,0,74
4,f,4/15/1947,8,1,21900,13200,98,190,0,57
5,m,2/9/1955,15,1,45000,21000,98,138,0,49

When the data is small,the program is running ok.

but increase the data,like 400 item. I get this error:

Message: 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.

 

I catch it!it is very interesting,when I use this code,it is running ok.

 static void Main(string[] args)
        {
            string filePath = System.Environment.CurrentDirectory + "\\data\\data.csv";           
            string input = "";
            using (StreamReader sr = new StreamReader(filePath))
            {
                string line = "";
                while ((line = sr.ReadLine()) != null)
                {
                    input += line + @"\r\n";
                }
            }

            REngine.SetDllDirectory(RHelper.GetRPath());
            REngine.CreateInstance("RDotNet", new[] { "-q" });
            REngine engine = REngine.GetInstanceFromID("RDotNet");
           
            
            string dataFrameExpression = "tb";
            // filePath =  filePath.Replace("\\","\\\\");
            DataFrame tb = engine.EagerEvaluate(dataFrameExpression + "<-read.table(text='" + input + "',sep=',',header=TRUE,as.is=TRUE,na.strings = '')").AsDataFrame();
           // DataFrame tb = engine.EagerEvaluate(dataFrameExpression + "<-read.csv(file='" + filePath + "',sep=',',header=TRUE,as.is=TRUE)").AsDataFrame();
            var result = engine.EagerEvaluate("lm.employee<-lm(salary~salbegin+jobtime+educ+jobcat,data=tb)").AsList();
            var summary = engine.EagerEvaluate("summary(lm.employee)");
            var confficients = engine.EagerEvaluate("lm.employee$coefficients").AsNumeric();
            Console.WriteLine(string.Join("\t", confficients.Names));
            Console.WriteLine(string.Join(" ", confficients));
            engine.Close();
            Console.Read();
        }

 Juest change the code layout,put the "read text" code before creat rengine instance.It's ok now,it's amazing.

but I don't know why,anyone can tell me?