From simon.urbanek at r-project.org  Thu Feb  5 18:35:58 2015
From: simon.urbanek at r-project.org (Simon Urbanek)
Date: Thu, 5 Feb 2015 12:35:58 -0500
Subject: [Rd] Invalid read of size 8
In-Reply-To: <1423140101020-4702843.post@n4.nabble.com>
References: <1423140101020-4702843.post@n4.nabble.com>
Message-ID: <E4945EBC-F260-47C0-B275-08CE757FFB4F@r-project.org>

As you can see from the stack trace the memory you're trying to use has already been released so it was not protected.
Since you only provided part of your code we can't really reproduce it or help you.

However, why don't you use just use

double *MatCovExtra = REAL(PROTECT(allocMatrix(REALSXP, pownLC, nT+1)));

instead the double-pointers?

Cheers,
Simon



> On Feb 5, 2015, at 7:41 AM, niandra <gianluca.mastrantonio at yahoo.it> wrote:
> 
> 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.
> 
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
> 



[Rd] Invalid read of size 8
Simon Urbanek