Invalid read of size 8

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Invalid read of size 8

niandra
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.
Reply | Threaded
Open this post in threaded view
|

Re: Invalid read of size 8

niandra
Few more details:

nSamples_save is an integer variable and if nSamples_save>1000 I get the error, while if nSamples_save<1000 everything is fine
Reply | Threaded
Open this post in threaded view
|

Re: Invalid read of size 8

Simon Urbanek
In reply to this post by niandra
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 <[hidden email]> 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.
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel