Re: [Rd] proper use of reg.finalizer to close connections

2014-10-27 Thread Murat Tasan
Eh, after some flailing, I think I solved it. I _think_ this pattern should guarantee that the finalizer function is still present when needed: .STATE_CONTAINER - new.env(parent = emptyenv()) .STATE_CONTAINER$some_state_variable - ## some code .STATE_CONTAINER$some_other_state_variable - ## some

Re: [Rd] proper use of reg.finalizer to close connections

2014-10-27 Thread Henrik Bengtsson
...and don't forget to make sure all the function that .myFinalizer() calls are also around. /Henrik On Mon, Oct 27, 2014 at 10:10 AM, Murat Tasan mmu...@gmail.com wrote: Eh, after some flailing, I think I solved it. I _think_ this pattern should guarantee that the finalizer function is still

Re: [Rd] proper use of reg.finalizer to close connections

2014-10-27 Thread Murat Tasan
yup... for context, the finalizer code calls functions from packages that are imported by my package. so, i think (unless something else has gone seriously wrong), those imported namespaces should still be available prior to my package's unloading. (and if imported namespaces are detached prior to

[Rd] proper use of reg.finalizer to close connections

2014-10-26 Thread Murat Tasan
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

Re: [Rd] proper use of reg.finalizer to close connections

2014-10-26 Thread Gábor Csárdi
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(e) #

Re: [Rd] proper use of reg.finalizer to close connections

2014-10-26 Thread Murat Tasan
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

Re: [Rd] proper use of reg.finalizer to close connections

2014-10-26 Thread Gábor Csárdi
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

Re: [Rd] proper use of reg.finalizer to close connections

2014-10-26 Thread Murat Tasan
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

Re: [Rd] proper use of reg.finalizer to close connections

2014-10-26 Thread Henrik Bengtsson
On Sun, Oct 26, 2014 at 8:14 PM, Murat Tasan mmu...@gmail.com 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