r-lib / cpp11

cpp11 helps you to interact with R objects using C++ code.
https://cpp11.r-lib.org/
Other
193 stars 46 forks source link

Custom deleter (with warning) for an external pointer #260

Closed nbenn closed 2 years ago

nbenn commented 2 years ago

Unfortunately, so far I have not been able to create a minimal reprex. I might be with some help and I'm happy to try harder. I was trying to add a warning to a custom deleter for a cpp11::external_pointer, roughly like so:

#include "cpp11.hpp"

struct my_struct {
  int a;
};

void my_struct_deleter(my_struct* x) {
  cpp11::warning("oh no!");
  delete x;
}

[[cpp11::register]] SEXP init_my_struct() {
  auto new_struct = new my_struct();
  cpp11::external_pointer<my_struct, my_struct_deleter> res(new_struct);
  return res;
}

The above is not a reprex, as this works fine. In my case, however, I get something like:

Error: segfault from C stack overflow
Warning message:
oh no!
libc++abi: terminating with uncaught exception of type cpp11::unwind_exception: std::exception

If I swap out cpp11::warning() with Rf_warning(), all is fine again. Before going into more detail I would like to ask a question (and if this is the wrong place for asking such a question, I apologize and will take it to SO).

Is what I am trying to do something that should be supported by cpp11 or is it somehow clear that this, in general, does not work well?

nbenn commented 2 years ago

After further trying (and failing) to reproduce my issue in a more contained setup, I'm closing this, as I think it might be triggered by something not directly to cpp11::external_pointer.