I currently write a DBI compliant database interface. The DBI framework stipulates a method ´dbClearResult´, as : "Frees all resources (local and remote) associated with a result set". I have an R RC object containing metadata on the connection and the DB result set, which I want to dispose so that garbage collection can take place. Writing an external method as
setMethod("dbClearResult", signature(res="MyResult"),
def=function(res) rm(res)
)
does of course not work as it deletes only its copy of res, not the object (copy-on-modify). Accessing the object from here via the method's parent environment might also not be a solution, for the reference name is unknown - one would have to give the method a character string instead of an object (signature="character"), which is not allowed by the DBI generic.
Therefore, I would like to implement a method into a R reference class that disposes its object and frees up resources in R, e.g. like:
t <- setRefClass("t", methods = list(
dispose = function(.self) {
rm(.self)
gc() # (optional)
})
)
test <- t$new()
test$dispose()
This code does not work as via .self the object can aparently only be read, not modified. So my idea is currently to search through the parent environment for references to the object and remove those.
Questions:
- Is there a better approach?
- How can I identify the references?