From gianluca.mastrantonio at yahoo.it  Thu Feb  5 13:41:41 2015
From: gianluca.mastrantonio at yahoo.it (niandra)
Date: Thu, 5 Feb 2015 04:41:41 -0800 (PST)
Subject: [Rd] Invalid read of size 8
Message-ID: <1423140101020-4702843.post@n4.nabble.com>

Hi,

I have a R program that call a C function.  I define a vector of pointer as


int nLC=3;
int pownLC = nLC*nLC
double *MatCovExtra[nT+1];
for(k=0;k<K+1;k++)
{
     MatCovExtra[k]         = (double*)R_alloc(pownLC, sizeof(double));

}

where nT>K. Then i put some values on the vector associated with the
pointer:

for(k=0;k<K+1;k++)
 {
                 for(i=0;i<nLC;i++)
                 {
                     for(j=i;j<nLC;j++)
                     {
                         MatCovExtra[k][i*nLC+j] = startSigma[i*nLC+j];
                     }
                 }
         }        
and i check that everithing is ok with

for(k=0;k<K+1;k++)
{
             Rprintf("First Cov Extra %i \n",k);
             for(i=0;i<nLC;i++)
             {
                 for(j=i;j<nLC;j++)
                 {
                     Rprintf("%f ", MatCovInv[k][i*nLC+j]);
                 }
                 Rprintf("\n");
             }
             Rprintf("\n\n");

}

After that i define an  R variable

int nProtect =0;
 SEXP zDP_out_r;

PROTECT(zDP_out_r          = allocMatrix(INTSXP, nT, nSamples_save));
nProtect++;
int *zDP_out_P             = INTEGER(zDP_out_r);



and using the same syntax as before

for(k=0;k<K+1;k++)
{
             Rprintf("Later Cov Extra %i \n",k);
             for(i=0;i<nLC;i++)
             {
                 for(j=i;j<nLC;j++)
                 {
                     Rprintf("%f ", MatCovInv[k][i*nLC+j]);
                 }
                 Rprintf("\n");
             }
             Rprintf("\n\n");

}
 
I see that the values of MatCovInv[k] have changed.

Valgrind shows the following error on the second block of Rprintf:


Later Cov Extra 3  0x107df7fb8
==954== Invalid read of size 8
==954==    at 0xA92D599: NPHDPHMM (in /Users/NPHDPHMM.so)
==954==    by 0x8632A: do_dotcall (dotcode.c:652)
==954==    by 0xB036A: Rf_eval (eval.c:657)
==954==    by 0xBE8C0: do_set (eval.c:2028)
==954==    by 0xB0404: Rf_eval (eval.c:629)
==954==    by 0xBE3C4: do_begin (eval.c:1638)
==954==    by 0xB0404: Rf_eval (eval.c:629)
==954==    by 0xBB95D: Rf_applyClosure (eval.c:1038)
==954==    by 0xB03BC: Rf_eval (eval.c:676)
==954==    by 0xBE8C0: do_set (eval.c:2028)
==954==    by 0xB0404: Rf_eval (eval.c:629)
==954==    by 0xE9012: Rf_ReplIteration (main.c:260)
==954==  Address 0x107df7fb8 is 40 bytes inside a block of size 176 free'd
==954==    at 0x4D8D: free (vg_replace_malloc.c:477)
==954==    by 0xF0BCA: R_gc_internal (memory.c:1026)
==954==    by 0xF2400: Rf_allocVector3 (memory.c:2578)
==954==    by 0x2BBCE: Rf_allocMatrix (Rinlinedfuns.h:189)
==954==    by 0xA92D449: NPHDPHMM (in /Users/NPHDPHMM.so)
==954==    by 0x8632A: do_dotcall (dotcode.c:652)
==954==    by 0xB036A: Rf_eval (eval.c:657)
==954==    by 0xBE8C0: do_set (eval.c:2028)
==954==    by 0xB0404: Rf_eval (eval.c:629)
==954==    by 0xBE3C4: do_begin (eval.c:1638)
==954==    by 0xB0404: Rf_eval (eval.c:629)
==954==    by 0xBB95D: Rf_applyClosure (eval.c:1038)
==954== 


Where is the error? I tried to search on the internet but i didn't find a
solution.



--
View this message in context: http://r.789695.n4.nabble.com/Invalid-read-of-size-8-tp4702843.html
Sent from the R devel mailing list archive at Nabble.com.



[Rd] Invalid read of size 8
niandra