ropensci / redland-bindings

Redland librdf language bindings
http://librdf.org/bindings/
Other
17 stars 3 forks source link

Review memory management in all methods #59

Open gothub opened 6 years ago

gothub commented 6 years ago

See if it is possible to automatically remove member instead of having to call the free*() methods that are included with most functions. Is this possible with methods that call a C library?

cboettig commented 6 years ago

Sounds like this is possible with C methods, you just need to define the appropriate finalizer. (see below, sorry posted this to the wrong thread earlier)

@gothub Looks like xml2 frees memory by taking advantage of helper utilities built into RCpp, see https://www.r-bloggers.com/external-pointers-with-rcpp/, but I'm not sure if that'll do any good for the redland C bindings.

However, @richfitz pointed me to the official R-ext docs on external pointers, which, if I've understood him correctly, will do much the same thing to handle removal of pointers via garbage collection. See: https://cran.r-project.org/doc/manuals/r-release/R-exts.html#External-pointers-and-weak-references

Rich has a working example of this in his packages, e.g.

here's an example from one of mine: https://github.com/richfitz/thor/blob/ff82d7/src/thor.c#L524-L530 (use) and https://github.com/richfitz/thor/blob/ff82d7/src/thor.c#L569-L575 (definition) - there's a declaration at the top of the file too. Follows the pattern in R-exts pretty closely but with the r_mdb_get_env function that checks for invalid pointer access too