<div dir="ltr">I ran your example under valgrind on Linux (Ubuntu 20.04) and valgrind found some memory misuse:<div><br></div><div>$ R --quiet --no-save --debugger=valgrind --debugger-args="--track-origins=yes"<br>==1533== Memcheck, a memory error detector<br>==1533== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.<br>==1533== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info<br>==1533== Command: /home/bill/R-devel/R-build/bin/exec/R --quiet --no-save<br>==1533==<br>> library(terra)<br>terra version 1.5.2<br>> library(raster)<br>Loading required package: sp<br>> library(GLCMTextures)<br>><br>> r1a<- raster::raster(volcano)<br>> r2a<- glcm_textures(r1a, w=c(3,7), n_levels = 16, quantization = "equal prob", shift=c(0,1))<br>><br>> r1b<- terra::rast(volcano)<br>> r2b<- glcm_textures2(r1b, w=c(3,7), n_levels = 16, quantization = "equal prob", shift=c(0,1)) #Often leads to Rsession Aborted<br>==1533== Invalid read of size 4<br>==1533==    at 0x1C1ADA01: C_make_glcm(Rcpp::Matrix<13, Rcpp::PreserveStorage>, int, Rcpp::Vector<13, Rcpp::PreserveStorage>, Rcpp::String) (glcm_cpp_functions.cpp:73)<br>==1533==    by 0x1C1B2237: C_glcm_textures_helper2(Rcpp::Vector<13, Rcpp::PreserveStorage>, Rcpp::Vector<13, Rcpp::PreserveStorage>, int, Rcpp::Vector<13, Rcpp::PreserveStorage>, Rcpp::String, unsigned long, unsigned long) (glcm_cpp_functions.cpp:218)<br>==1533==    by 0x1C1A098D: _GLCMTextures_C_glcm_textures_helper2 (RcppExports.cpp:94)<br>==1533==    by 0x1F6B01: R_doDotCall (dotcode.c:622)<br>==1533==    by 0x2637C9: bcEval (eval.c:7695)<br>==1533==    by 0x240653: Rf_eval (eval.c:748)<br>==1533==    by 0x243223: R_execClosure (eval.c:1918)<br>==1533==    by 0x242EFD: Rf_applyClosure (eval.c:1844)<br>==1533==    by 0x240E06: Rf_eval (eval.c:871)<br>==1533==    by 0x246CD6: do_set (eval.c:2990)<br>==1533==    by 0x240A8C: Rf_eval (eval.c:823)<br>==1533==    by 0x2457DA: do_begin (eval.c:2538)<br>==1533==  Address 0xa59c0c4 is 28 bytes before a block of size 64 in arena "client"<br>==1533==<br>==1533== Invalid write of size 4<br>==1533==    at 0x1C1ADA44: C_make_glcm(Rcpp::Matrix<13, Rcpp::PreserveStorage>, int, Rcpp::Vector<13, Rcpp::PreserveStorage>, Rcpp::String) (glcm_cpp_functions.cpp:73)<br>==1533==    by 0x1C1B2237: C_glcm_textures_helper2(Rcpp::Vector<13, Rcpp::PreserveStorage>, Rcpp::Vector<13, Rcpp::PreserveStorage>, int, Rcpp::Vector<13, Rcpp::PreserveStorage>, Rcpp::String, unsigned long, unsigned long) (glcm_cpp_functions.cpp:218)<br>==1533==    by 0x1C1A098D: _GLCMTextures_C_glcm_textures_helper2 (RcppExports.cpp:94)<br>==1533==    by 0x1F6B01: R_doDotCall (dotcode.c:622)<br>==1533==    by 0x2637C9: bcEval (eval.c:7695)<br>==1533==    by 0x240653: Rf_eval (eval.c:748)<br>==1533==    by 0x243223: R_execClosure (eval.c:1918)<br>==1533==    by 0x242EFD: Rf_applyClosure (eval.c:1844)<br>==1533==    by 0x240E06: Rf_eval (eval.c:871)<br>==1533==    by 0x246CD6: do_set (eval.c:2990)<br>==1533==    by 0x240A8C: Rf_eval (eval.c:823)<br>==1533==    by 0x2457DA: do_begin (eval.c:2538)<br>==1533==  Address 0xa59c0c4 is 28 bytes before a block of size 64 in arena "client"<br>==1533==<br>--1533-- VALGRIND INTERNAL ERROR: Valgrind received a signal 11 (SIGSEGV) - exiting<br>--1533-- si_code=1;  Faulting address: 0x10A59C138;  sp: 0x10090e2e20<br><br>valgrind: the 'impossible' happened:<br>   Killed by fatal signal<br><br>host stacktrace:<br>==1533==    at 0x580505C4: ??? (in /usr/lib/x86_64-linux-gnu/valgrind/memcheck-amd64-linux)<br>==1533==    by 0x58004EBB: ??? (in /usr/lib/x86_64-linux-gnu/valgrind/memcheck-amd64-linux)<br>==1533==    by 0x58005DA7: ??? (in /usr/lib/x86_64-linux-gnu/valgrind/memcheck-amd64-linux)<br>==1533==    by 0x580A7204: ??? (in /usr/lib/x86_64-linux-gnu/valgrind/memcheck-amd64-linux)<br>==1533==    by 0x580F5FD4: ??? (in /usr/lib/x86_64-linux-gnu/valgrind/memcheck-amd64-linux)<br><br>sched status:<br>  running_tid=1<br><br>Thread 1: status = VgTs_Runnable (lwpid 1533)<br>==1533==    at 0x483BE63: operator new(unsigned long) (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)<br>==1533==    by 0x6E92D3C: void std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_construct<char const*>(char const*, char const*, std::forward_iterator_tag) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.28)<br>==1533==    by 0x1C1B2377: C_glcm_textures_helper2(Rcpp::Vector<13, Rcpp::PreserveStorage>, Rcpp::Vector<13, Rcpp::PreserveStorage>, int, Rcpp::Vector<13, Rcpp::PreserveStorage>, Rcpp::String, unsigned long, unsigned long) (glcm_cpp_functions.cpp:221)<br>==1533==    by 0x1C1A098D: _GLCMTextures_C_glcm_textures_helper2 (RcppExports.cpp:94)<br>==1533==    by 0x1F6B01: R_doDotCall (dotcode.c:622)<br>==1533==    by 0x2637C9: bcEval (eval.c:7695)<br>==1533==    by 0x240653: Rf_eval (eval.c:748)<br>==1533==    by 0x243223: R_execClosure (eval.c:1918)<br>==1533==    by 0x242EFD: Rf_applyClosure (eval.c:1844)<br>==1533==    by 0x240E06: Rf_eval (eval.c:871)<br>==1533==    by 0x246CD6: do_set (eval.c:2990)<br>==1533==    by 0x240A8C: Rf_eval (eval.c:823)<br>==1533==    by 0x2457DA: do_begin (eval.c:2538)<br>==1533==    by 0x240A8C: Rf_eval (eval.c:823)<br>==1533==    by 0x244FC9: do_for (eval.c:2420)<br>==1533==    by 0x240A8C: Rf_eval (eval.c:823)<br>==1533==    by 0x2457DA: do_begin (eval.c:2538)<br>==1533==    by 0x240A8C: Rf_eval (eval.c:823)<br>==1533==    by 0x244FC9: do_for (eval.c:2420)<br>==1533==    by 0x240A8C: Rf_eval (eval.c:823)<br>==1533==    by 0x2457DA: do_begin (eval.c:2538)<br>==1533==    by 0x240A8C: Rf_eval (eval.c:823)<br>==1533==    by 0x243223: R_execClosure (eval.c:1918)<br>==1533==    by 0x242EFD: Rf_applyClosure (eval.c:1844)<br>==1533==    by 0x253F32: bcEval (eval.c:7107)<br>==1533==    by 0x240653: Rf_eval (eval.c:748)<br>==1533==    by 0x243223: R_execClosure (eval.c:1918)<br>==1533==    by 0x243D9C: R_execMethod (eval.c:2094)<br>==1533==    by 0x8800187: R_dispatchGeneric (methods_list_dispatch.c:1145)<br>==1533==    by 0x2B15F4: do_standardGeneric (objects.c:1285)<br>==1533==    by 0x253DB3: bcEval (eval.c:7096)<br>==1533==    by 0x240653: Rf_eval (eval.c:748)<br>==1533==    by 0x243223: R_execClosure (eval.c:1918)<br>==1533==    by 0x242EFD: Rf_applyClosure (eval.c:1844)<br>==1533==    by 0x253F32: bcEval (eval.c:7107)<br>==1533==    by 0x240653: Rf_eval (eval.c:748)<br>==1533==    by 0x243223: R_execClosure (eval.c:1918)<br>==1533==    by 0x242EFD: Rf_applyClosure (eval.c:1844)<br>==1533==    by 0x240E06: Rf_eval (eval.c:871)<br>==1533==    by 0x246CD6: do_set (eval.c:2990)<br>==1533==    by 0x240A8C: Rf_eval (eval.c:823)<br>==1533==    by 0x28EF5E: Rf_ReplIteration (main.c:264)<br>==1533==    by 0x28F157: R_ReplConsole (main.c:316)<br>==1533==    by 0x290BC0: run_Rmainloop (main.c:1130)<br>==1533==    by 0x290BDA: Rf_mainloop (main.c:1137)<br>==1533==    by 0x16F74B: main (Rmain.c:29)<br>client stack range: [0x1FFEED3000 0x1FFF000FFF] client SP: 0x1FFEFF35F0<br>valgrind stack range: [0x1008FE3000 0x10090E2FFF] top usage: 12336 of 1048576<br><br><br>Note: see also the FAQ in the source distribution.<br>It contains workarounds to several common problems.<br>In particular, if Valgrind aborted or crashed after<br>identifying problems in your program, there's a good chance<br>that fixing those problems will prevent Valgrind aborting or<br>crashing, especially if it happened in m_mallocfree.c.<br><br>If that doesn't help, please report this bug to: <a href="http://www.valgrind.org">www.valgrind.org</a><br><br>In the bug report, send all the above text, the valgrind<br>version, and what OS and version you are using.  Thanks.<br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Mon, Dec 13, 2021 at 8:15 AM Alexander Ilich <<a href="mailto:ailich@mail.usf.edu">ailich@mail.usf.edu</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div>Hi, I'm upgrading one of my R packages to rely on the terra package instead of the raster package for the handling of spatial raster data. Previously when relying on the raster package I had to convert the data to a matrix and send it to C++ to loop through the cells manually, but with the new features in the terra package I can supply a C++ function that returns multiple values directly to terra::focalCpp to perform <a href="https://desktop.arcgis.com/en/arcmap/10.3/tools/spatial-analyst-toolbox/how-focal-statistics-works.htm" target="_blank">focal operations</a>. I get the same values using both the old and new versions of the functions, but the new version often causes the R session to abort, especially at larger window sizes. For example, a 3x3 or a 3x5 window seems to always run, but a 3x7 window will often cause the R session to abort. However, when it doesn't, I get the correct values. Functions followed by 2 are the terra versions of the function. By commenting out things and rebuilding I was able to determine the line that causes the crash in the terra version is "NumericMatrix curr_GLCM = C_make_glcm(curr_window, n_levels, shift, na_opt); //Tabulate the GLCM"; however, this line is also included in the raster version of the function so I'm not sure why this would happen. Any help would be greatly appreciated. Here is the <a href="https://github.com/ailich/GLCMTextures/tree/terra" target="_blank">github repository</a>, and I've added some sample code below to illustrate the issue.</div><div><br></div><div>Thanks,</div><div>Alex</div><div><br></div><div>install.packages('raster', repos='<a href="https://rspatial.r-universe.dev" target="_blank">https://rspatial.r-universe.dev</a>') #install development version of raster<br>install.packages('terra', repos='<a href="https://rspatial.r-universe.dev" target="_blank">https://rspatial.r-universe.dev</a>') #install development version of terra<br><br>remotes::install_github("ailich/GLCMTextures", ref = "terra") #Install branch of my package testing terra versions of functions<br><br>library(terra)<br>library(raster)<br>library(GLCMTextures)<br><br>r1a<- raster::raster(volcano)<br>r2a<- glcm_textures(r1a, w=c(3,7), n_levels = 16, quantization = "equal prob", shift=c(0,1))<br><br>r1b<- terra::rast(volcano)<br>r2b<- glcm_textures2(r1b, w=c(3,7), n_levels = 16, quantization = "equal prob", shift=c(0,1)) #Often leads to Rsession Aborted<br><br>all.equal(values(r2a),values(r2b)) #TRUE<br><br><br><br>#System Information<br>#OS: Windows 10<br>R.version<br># platform       x86_64-w64-mingw32<br># arch           x86_64<br># os             mingw32<br># system         x86_64, mingw32<br># status<br># major          4<br># minor          0.4<br># year           2021<br># month          02<br># day            15<br># svn rev        80002<br># language       R<br># version.string R version 4.0.4 (2021-02-15)<br># nickname       Lost Library Book<br><br>packageVersion("terra")<br># ‘1.5.2’<br>packageVersion("raster")<br># ‘3.5.10’<br>packageVersion("Rcpp")<br># ‘1.0.7’<br>packageVersion("RcppArmadillo")<br># ‘0.10.7.3.0’</div></div>
_______________________________________________<br>
Rcpp-devel mailing list<br>
<a href="mailto:Rcpp-devel@lists.r-forge.r-project.org" target="_blank">Rcpp-devel@lists.r-forge.r-project.org</a><br>
<a href="https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel" rel="noreferrer" target="_blank">https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel</a></blockquote></div>