[Rcpp-devel] R Session Aborted when calling C++

Gregory Jefferis jefferis at mrc-lmb.cam.ac.uk
Thu Oct 29 08:47:15 CET 2020


But thinking about it again that’s probably suboptimal but not fatal. 
However these lines immediately before that if statement look like you 
are accessing the input array x *before* checking bounds

       neighbor_idx(0) = i-shift(1);
       neighbor_idx(1) = j+shift(0);
       int neighbor_val = x(neighbor_idx(0), neighbor_idx(1));

So this should fix, I think:

       if((neighbor_idx(0) < nr) && (neighbor_idx(1) < nc) &&
          (neighbor_idx(0) >= 0) && (neighbor_idx(1) >= 0)){
         int neighbor_val = x(neighbor_idx(0), neighbor_idx(1));
         GLCM(focal_val,neighbor_val) = GLCM(focal_val,neighbor_val)+1;
         GLCM(neighbor_val,focal_val) = GLCM(neighbor_val,focal_val)+1;
       }

Best,

Greg.

https://github.com/ailich/GLCMTextures/blob/2d69b5b71c558b89463e4726140d3378f5674fa6/src/glcm_cpp_functions.cpp#L47-L53


Sent from my iPhone

> On 29 Oct 2020, at 06:57, Gregory Jefferis 
> <jefferis at mrc-lmb.cam.ac.uk> wrote:
>
> Alexander,
>
> I think your bounds checking code uses bitwise & rather than logical 
> &&
>
>     if((neighbor_idx(0) < nr) & (neighbor_idx(1) < nc) & 
> (neighbor_idx(0) >= 0) & (neighbor_idx(1) >= 0)){
>        GLCM(focal_val,neighbor_val) = GLCM(focal_val,neighbor_val)+1;
>        GLCM(neighbor_val,focal_val) = GLCM(neighbor_val,focal_val)+1;
>      }
>
>
> Best,
>
> Greg.
>
> Sent from my iPhone
>
>> On 29 Oct 2020, at 00:15, Dirk Eddelbuettel <edd at debian.org> wrote:
>>
>> 
>> Alexander,
>>
>> A segmentation fault can take the session down as it is undefined 
>> behavior,
>> yet is almost always the error of the programmers.
>>
>> We have written the basic accessors for efficiency so they do not 
>> check. But
>> there are checking accessor that throw with a proper message:
>>
>> R> Rcpp::cppFunction("void foo(IntegerVector x) { int n = x.size(); 
>> Rcpp::Rcout << x.at(n+1) << std::endl; }")
>> R> foo(1:3)
>> Error in foo(1:3) : Index out of bounds: [index=4; extent=3].
>> R>
>>
>> No segfault, just a clean error message. RcppArmadillo has something 
>> similar.
>>
>> By contrast if you don't check you can get garbage or bad behavior:
>>
>> R> Rcpp::cppFunction("void bar(IntegerVector x) { int n = x.size(); 
>> Rcpp::Rcout << x[n+1] << std::endl; }")
>> R> bar(1:3)
>> 1358954573
>> R>
>>
>> No crash but not exactly 'right' either.
>>
>> The other thing you can do is to couple R with gdb to run under the 
>> debugger
>> to get access to your indexing variables. There are some writeups in
>> different places as it takes two steps---but may be worth it. Here 
>> are two
>> quick hits from StackOverflow:
>>
>> https://stackoverflow.com/questions/21226337/what-are-productive-ways-to-debug-rcpp-compiled-code-loaded-in-r-on-os-x-maveri
>> https://stackoverflow.com/questions/11345537/debugging-line-by-line-of-rcpp-generated-dll-under-windows
>>
>> Dirk
>>
>> -- 
>> https://dirk.eddelbuettel.com | @eddelbuettel | edd at debian.org
>> _______________________________________________
>> Rcpp-devel mailing list
>> Rcpp-devel at lists.r-forge.r-project.org
>> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
> _______________________________________________
> Rcpp-devel mailing list
> Rcpp-devel at lists.r-forge.r-project.org
> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel


--
Gregory Jefferis, PhD                   Tel: +44 1223 267048
Division of Neurobiology
MRC Laboratory of Molecular Biology
Francis Crick Avenue
Cambridge Biomedical Campus
Cambridge, CB2 0QH, UK

http://www2.mrc-lmb.cam.ac.uk/group-leaders/h-to-m/g-jefferis
http://jefferislab.org
http://www.zoo.cam.ac.uk/research/groups/connectomics


More information about the Rcpp-devel mailing list