Coder Social home page Coder Social logo

Comments (11)

ctoney avatar ctoney commented on June 26, 2024 1

From https://stackoverflow.com/questions/59384221/proper-way-to-return-a-pointer-to-a-new-object-from-an-rcpp-function

Rcpp Modules register a default finalizer that calls the object's destructor. You can also add a custom finalizer if needed.

Sorry, I didn't catch that before. My rationale, originally, was that a custom finalizer would run upon gc() but the class destructor may be less predictable. If the above is correct, and the destructor does the necessary clean-up, then a custom finalizer may not be needed. Makes sense.

It seems to work fine either way for class VSIFile, and the destructor is called during garbage collection when not using .finalizer. I'll omit the custom finalizer in this case.

from rcpp.

ctoney avatar ctoney commented on June 26, 2024 1

Intended to circle back and let you know it is on CRAN but got distracted by a couple of things. My issue is resolved, but by not using the custom finalizer. The object's destructor is called at gc(), since Rcpp Modules registers a default finalizer as noted above (but I missed that previously). That meets my need and the cleanup code is very simple. The compiler warning appears though, if a custom .finalizer is defined on the exposed class, in case that's of interest. Thanks again.

from rcpp.

eddelbuettel avatar eddelbuettel commented on June 26, 2024

Good question. Modules has not seen a lot of love those last few years.

If your repo with added finalizer public? Not sure we have (m)any other examples of finalizers in use (though I have not looked yet).

from rcpp.

kevinushey avatar kevinushey commented on June 26, 2024

I think you're right; the class has virtual methods but no virtual destructor:

template <typename Class>
class CppFinalizer{
public:
CppFinalizer(){} ;
virtual void run(Class* ){} ;
} ;

We should probably add it, or just simplify and remove the virtual inheritance used here (since FunctionFinalizer is the only thing that inherits from CppFinalizer)

from rcpp.

eddelbuettel avatar eddelbuettel commented on June 26, 2024

What do you think about e.g. rule of four adding the missing ones as defaults? (I am spitballing here...)

from rcpp.

ctoney avatar ctoney commented on June 26, 2024

Thanks for the quick response.

If your repo with added finalizer public?

It's currently in a branch of my fork here, but will be merged sometime soon.
https://github.com/ctoney/gdalraster/blob/vsifile/src/vsifile.h
https://github.com/ctoney/gdalraster/blob/vsifile/src/vsifile.cpp

I have a couple of other exposed classes that I would add finalizers for, but didn't have it working in the past so spending a little more time to check it out now.

Modules has not seen a lot of love those last few years.

FWIW, I really like modules and RCPP_EXPOSED_CLASS. Super useful and nice to work with. Thanks!

from rcpp.

eddelbuettel avatar eddelbuettel commented on June 26, 2024

Do you want to give modifying the set of defaults and see where it leads you?

from rcpp.

ctoney avatar ctoney commented on June 26, 2024

update to the above, the class with finalizer has been merged in main:
https://github.com/USDAForestService/gdalraster/blob/main/src/vsifile.h
https://github.com/USDAForestService/gdalraster/blob/main/src/vsifile.cpp

from rcpp.

eddelbuettel avatar eddelbuettel commented on June 26, 2024

That's actually a pretty nice example for modules use!

from rcpp.

ctoney avatar ctoney commented on June 26, 2024

It's a nice API to be able to wrap and use from R. Class VSIFile is in pretty good shape now I think, and has a decent set of tests for /vsimem/ at least. There is also a set of Rcpp exported functions for file system operations through GDAL VSI. Among other things, these fully abstract file system operations for cloud storage services.
https://usdaforestservice.github.io/gdalraster/reference/index.html#virtual-file-systems

And as side note, VSIFile was initial use of RcppInt64 for this package. It made that part easy too. I was glad to add it and start using for int64 in a few other places as well.

from rcpp.

eddelbuettel avatar eddelbuettel commented on June 26, 2024

Hey @ctoney just circling back and as you (per CRANberries) have a new version on CRAN: are you all good here? What if any action items do we have left?

from rcpp.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.