[Rcpp-devel] Segfault in wrapping code in Rcpp

Nikhil Garg nikhilgarg.gju at gmail.com
Sat Mar 2 08:51:47 CET 2024


Hi,

I am currently working on wrapping some code from a C++ library. I am using
RCPP for this work. I have managed to get the wrapping code compiled but I
get segfault when I try to use some of the functionality.

Here is an example of how I have gone about it, though I think I am quite
sure I am missing something. Note, I have left out methods in the
RCPP_MODULE wrappings. I am happy to share that if needed to get more
information.

In the code below, *RasterBase* is an abstract class that derives from
*PropertyMap.* I have used Romain Francis's suggestion (
https://stackoverflow.com/questions/24317910/rcpp-module-for-inheritance-class?rq=3)
on how to work with derived class and Ralf Stubner's suggestion (
https://stackoverflow.com/questions/54469409/how-to-expose-a-pointer-of-an-abstract-class-using-rcpp)
on how to work with an abstract class and got most stuff working.

typedef std::reference_wrapper<RasterBaseFloat> RasterBaseRef;
typedef std::vector<RasterBaseRef> RasterBaseRefs;

RasterBaseFloat *newRasterBase() {
return new RasterFloat();
}

void sortColumns(RasterFloat& r) {
Geostack::sortColumns(r);
}

RasterBaseFloat& get_raster_ref_from_vec(RasterBaseRefs &v,
std::size_t i)
{
return v[i].get();
}

void add_ref_to_vec(RasterBaseRefs &v, RasterBaseFloat &rf)
{
v.emplace_back(std::ref(rf));
}

void runScriptNoOut(std::string script, SEXP r_list, std::size_t param) {
Rcpp::List raster_base_list(r_list);
int n = raster_base_list.size();

RasterBaseRefs raster_list;
raster_list.reserve(n);
Rcpp::Rcout << raster_list.size() << std::endl;

for (int i = 0; i < n; i++) {
RasterFloat r = raster_base_list[i];
add_ref_to_vec(raster_list, r);
}

Rcpp::Rcout << raster_list.size() << std::endl;

Geostack::runScriptNoOut(script, raster_list, param);
}

RCPP_MODULE(GS_Raster) {
class_<PropertyMap>("PropertyMap")
.constructor("Instantiate property map object")

class_<RasterBaseFloat>("RasterBase")
.derives<PropertyMap>("PropertyMap")
.factory(&newRasterBase)

class_<RasterFloat>("Raster")
.derives<RasterBaseFloat>("RasterBase")

Here is how I have tested the wrappings in R.

library(devtools)
setwd("/home/gar305/Documents/geostack/r_binding/RcppGeostack")
devtools::load_all()
library(Rcpp)

testA <- new(Raster, "testRaster")
testA$init(256, 256, 4, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0)
testA$setAllCellValues(0.0)
testA$getCellValue(10, 10, 0)

dims <- testA$getRasterDimensions()
dims

projParams <- fromEPSG("4326")

testA$setProjectionParameters(projParams)

proj2 <- testA$getProjectionParameters()
proj2

testA$getProperty_String("name")

r_list <- list(testA)
runScriptNoOut("testRaster = randomNormal(0, 1);", r_list, 0)
testA$getCellValue(10, 10, 0)


and here is what I get when I run the above R code

[image: image.png]

Any help or suggestions on ways to rectify this issue would be great.

Regards,
Nikhil
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20240302/e23ce863/attachment-0001.htm>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image.png
Type: image/png
Size: 45541 bytes
Desc: not available
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20240302/e23ce863/attachment-0001.png>


More information about the Rcpp-devel mailing list