Hi all, I have a question about finalizers...
I have a package that manages state for a few connections, and I'd
like to ensure that these connections are 'cleanly' closed upon either
(i) R quitting or (ii) an unloading of the package.
So, in a pared-down example package with a single R file, it lo
Hmmm, I guess you will want to put the actual objects that represent
the connections into the environment, at least this seems to be the
easiest to me. Btw. you need ls() to list the contents of an
environment, instead of names(). E.g.
e <- new.env()
e$foo <- 10
e$bar <- "aaa"
names(e)
#> NULL
ls(
Ah, thanks for the ls() vs names() tip!
(But sadly, it didn't solve the issue... )
So, after some more tinkering, I believe the finalizer is being called
_sometimes_.
I changed the reg.finalizer(...) call to just this:
reg.finalizer(.CONNS, function(x) print("foo"), onexit = TRUE)
Now, when I l
Well, to be honest I don't understand fully what you are trying to do.
If you want to run code when the package is detached or when it is
unloaded, then use a hook:
http://cran.r-project.org/doc/manuals/r-devel/R-exts.html#Load-hooks
If you want to run code when an object is freed, then use a fina
Ah (again)!
Even with my fumbling presentation of the issue, you gave me the hint
that solved it, thanks!
Yes, the reg.finalizer call needs to be wrapped in an .onLoad hook so
it's not called once during package installation and then never again.
And once I switched to using ls() (instead of names
On Sun, Oct 26, 2014 at 8:14 PM, Murat Tasan wrote:
> Ah (again)!
> Even with my fumbling presentation of the issue, you gave me the hint
> that solved it, thanks!
>
> Yes, the reg.finalizer call needs to be wrapped in an .onLoad hook so
> it's not called once during package installation and then
Ah, good point, I hadn't thought of that detail.
Would moving reg.finalizer back outside of .onLoad and hooking it to the
package's environment itself work (more safely)?
Something like:
finalizerFunction <- ## cleanup code
reg.finalizer(parent.env(), finalizerFunction)
-m
On Oct 26, 2014 11:03 P