This project is read-only.

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

May 7, 2015 at 4:01 PM
Hello i use R.NET 1.6 on my program. I got error message : "Attempted to read or write protected memory. This is often an indication that other memory is corrupt." when i tried to run a function from a script. Here's my code :
DataTable dt = GetFromMainForm();
            REngine.SetEnvironmentVariables();
            REngine engine = REngine.GetInstance();
            List<double> t = new List<double>();
            int nkol = selectedListBox.Items.Count;
            int kol1 = dt.Columns[selectedListBox.Items[0].ToString()].Ordinal;
            for (int i = 0; i < nkol; i++)
            {
                String row = dt.Rows[i].ItemArray[kol1].ToString();
                if (String.IsNullOrEmpty(row))
                {
                    break;
                }
                else
                {
                    double r = Convert.ToDouble(row);
                    t.Add(r);
                }
            }
            NumericVector u = engine.CreateNumericVector(t);
            engine.SetSymbol("matdt", u);
            NumericMatrix matdata = engine.Evaluate("matdt").AsNumericMatrix();
            if (nkol > 1)
            {
                for (int j = 1; j < nkol; j++)
                {
                    //j = j + 1;
                    int kol = dt.Columns[selectedListBox.Items[j].ToString()].Ordinal;
                    List<double> s = new List<double>();
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        String row = dt.Rows[i].ItemArray[kol].ToString();
                        if (String.IsNullOrEmpty(row))
                        {
                            break;
                        }
                        else
                        {
                            double r = Convert.ToDouble(row);
                            s.Add(r);
                        }
                    }
                    String nokol = Convert.ToString(j + 1);
                    NumericVector v = engine.CreateNumericVector(s);
                    engine.SetSymbol("dum", v);
                    matdata = engine.Evaluate("matdt=matrix(cbind(matdt,dum),ncol=" + nokol + ")").AsNumericMatrix();
                }
            }
            String nclust = cTextBox.Text;
            NumericVector c = engine.Evaluate(nclust).AsNumeric();
            engine.SetSymbol("c", c);
            String fuzzifier = mtextBox.Text;
            NumericVector m = engine.Evaluate(fuzzifier).AsNumeric();
            engine.SetSymbol("m", m);
            String maxiter = mittextBox.Text;
            NumericVector mit = engine.Evaluate(maxiter).AsNumeric();
            engine.SetSymbol("mit", mit);
            String exerror = etextBox.Text;
            NumericVector err = engine.Evaluate(exerror).AsNumeric();
            engine.SetSymbol("e", err);

            String sc = "source('";
            String parent = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.Parent.FullName;
            String pd = parent.Replace("\\", Regex.Escape("/"));
            String cd = "/R-Cluzzy/Script R/fcm.R')";
            String scriptpath = sc + pd + cd;
            engine.Evaluate(scriptpath);

            NumericMatrix res = engine.Evaluate("fcm(matdt,c,m,mit,e)").AsNumericMatrix();
            engine.SetSymbol("result", res);
            NumericMatrix cen = engine.Evaluate("result$'Pusat Kelompok'").AsNumericMatrix();
            DataTable center = new DataTable();
            for (int i = 0; i < cen.ColumnCount; ++i)
            {
                center.Columns.Add();
            }
            for (int k = 0; k < cen.RowCount; ++k)
            {
                center.Rows.Add();
            }

            for (int i = 0; i < cen.RowCount; ++i)
            {
                for (int k = 0; k < cen.ColumnCount; ++k)
                {
                    center.Rows[i].SetField(k, cen[i, k]);
                }
            }
            mf.SetNormalization(center, selectedListBox);


        }
I got error message at "NumericMatrix res = engine.Evaluate("fcm(matdt,c,m,mit,e)").AsNumericMatrix();".
Here's the scipt that i ran :
fcm<-function(data,c,m,maxiter,e){
#inisialisasi random matriks U
U=matrix(ncol=c,nrow=nrow(data))
 for(i in 1:nrow(data)){
   for(j in 1:c){
     U[i,j]=runif(1)
   }
 }
 
#normalisasi matriks U
totU=matrix(rowSums(U),ncol=1,nrow=nrow(U))
for(a in 1:nrow(U)){
for(b in 1:ncol(U)){
U[a,b]=U[a,b]/totU[a,]
}
}

#Fungsi objektif awal
P=0 
iter=1 

repeat{
#Matriks pusat cluster
V=matrix(ncol=ncol(data),nrow=c)

#Menghitung pusat cluster
d=matrix(ncol=ncol(data),nrow=nrow(data))
Uk=matrix(ncol=ncol(U),nrow=nrow(U))
for(g in 1:nrow(U)){
for(h in 1:ncol(U)){
Uk[g,h]=(U[g,h])^(m)
}
}
Uks=matrix(colSums(Uk),ncol=ncol(Uk),nrow=1)
for(k in 1:c){
for(j in 1:nrow(data)){
d[j,]=(Uk[j,k])*data[j,]
}
V[k,]=colSums(d)/Uks[,k]
}

#Menghitung jarak data dengan pusat cluster
dist=matrix(ncol=1,nrow=nrow(data))
alldist=matrix(ncol=c,nrow=nrow(data))
for(l in 1:c){
for(n in 1:nrow(data)){
dist[n,]=t(data[n,]-V[l,])%*%(data[n,]-V[l,])
}
alldist[,l]=dist
}

#Menghitung fungsi objektif
ud=matrix(ncol=c,nrow=nrow(data))
for(o in 1:nrow(data)){
for(p in 1:c){
ud[o,p]=alldist[o,p]*Uk[o,p]
}
}

Pi=sum(rowSums(ud))
#Menghitung selisih fungsi objektif dengan fungsi objektif sebelumnya
P=abs(Pi-P)

#Menghitung perubahan matriks U
alldist2=matrix(ncol=ncol(alldist),nrow=nrow(alldist))
for(q in 1:nrow(alldist)){
for(r in 1:ncol(alldist)){
alldist2[q,r]=(alldist[q,r])^((-1)/(m-1))
}
}

alldist2[alldist2==Inf] <- 0 
totdist=matrix(rowSums(alldist2),ncol=1,nrow=nrow(alldist2))

for(s in 1:nrow(U)){
for(t in 1:ncol(U)){
U[s,t]=alldist2[s,t]/totdist[s,]
}
}

#Jika ada distance yang 0 maka nilai U-nya =1
nilnol<-which(U==0,arr.in=TRUE)
if(nrow(nilnol)>0){
  nilnol=matrix(nilnol,ncol=ncol(nilnol))
  for(nn in 1:nrow(nilnol)){
    U[nilnol[nn,1],]=0
    U[nilnol[nn,1],nilnol[nn,2]]=1
  }
}

iter=iter+1

#Mengecek kondisi berhenti
if(P<e | iter>maxiter){
break
}

}

result=list("Pusat Kelompok"=V,"Matriks Keanggotaan"=U,"Jumlah Iterasi"=iter-1)
return(result) 
}

Does anybody know how to solve this? Is this R.NET bug or something?
Jul 15, 2015 at 4:27 AM
Same here, when I call Rengine multiple times it will frequently throw this type of error “Attempted to read or write protected memory. This is often an indication that other memory is corrupt”, any here will be appreciated
Jul 16, 2015 at 1:55 AM
Ok, I may have found a solution to this. First I added in GC.Collect() in C# to force collect garbage. Then I set a timeout of about 1 second between each loop to let the system fully build up the connection between R and Asp.net. The error never appeared again. Now I'm not sure of the underlying mechanism, but this seems to work fine for me for now.