Thanks a lot! Also, for suggesting using `.Call`. I will try all three and follow up on the relevant list if I hit a dead-end.
Best wishes, Pantelis -----Original Message----- From: Dirk Eddelbuettel <e...@debian.org> Sent: Wednesday, May 24, 2023 4:16 PM To: Ivan Krylov <krylov.r...@gmail.com> Cc: pikappa.de...@gmail.com; r-devel@r-project.org Subject: Re: [Rd] Heap access across multiple calls from R to C++ On 24 May 2023 at 09:09, Ivan Krylov wrote: | On Wed, 24 May 2023 02:08:25 +0200 | <pikappa.de...@gmail.com> wrote: | > Is there a better way to do this? | | The .Call() interface (where functions take an arbitrary number of | native R objects and return a native R object) combined with external | pointers is likely to be a better approach here: | | https://cran.r-project.org/doc/manuals/R-exts.html#External-pointers-a | nd-weak-references Yes. We had numerous threads here and on r-package-devel (better list for the question IMHO, maybe follow-up there?) stating that new code really should avoid .C() and stick with .Call(). | On the R side, the returned SEXP will have class 'externalptr', with | not much to do about it programmatically. C code can additionally | register a finalizer on the object in order to release the memory | automatically after the it is discarded. (And Rcpp::XPtr sets those for you if you define RCPP_USE_FINALIZE_ON_EXIT.) A simpler approach is of course available too, and I used it too. Define your class as you would in C++. Then define - a helper function to initialize a class object via `new`, assign it to a static variable or a global pointer - helper functions for setters and getters of the values you need - a teardown function you can call, you may even tie it to R's on.exit() Now _you_ control lifetime and allocation. From R. If anything breaks, you get to keep the pieces. We can also help on the rcpp-devel list. Cheers, Dirk -- dirk.eddelbuettel.com | @eddelbuettel | e...@debian.org ______________________________________________ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel