Is there some way to prevent finalizers running during a section of code? I have a package that includes R objects linked to database tables. To maintain the call-by-value semantics, tables are copied rather than modified, and the extra tables are removed by finalizers during garbage collection. However, if the garbage collection occurs in the middle of processing another SQL query (which is relatively likely, since that's where the memory allocations are) there are problems with the database interface. Since the guarantees for the finalizer are "at most once, not before the object is out of scope" it seems harmless to be able to prevent finalizers from running during a particular code block, but I can't see any way to do it. Suggestions? -thomas -- Thomas Lumley Professor of Biostatistics University of Auckland [[alternative HTML version deleted]]
It might help if you could be more specific about what the issue is -- if they are out of scope why does it matter whether the finalizers run? Generically two approaches I can think of: you keep track of whenit is safe to fully run your finalizers and have your finalizers put the objects on a linked list if it isn't safe to run the finalizer now and clear the list each time you make a new one keep track of your objects with a weak list andturn them into strong references before your calls, then drop the list after. I'm pretty sure we don't have a mechanism for temporarily suspending running the finalizers but it is probably fairly easy to add if that is the only option. I might be able to think of other options with more details on the issue. Best, luke On Tue, 12 Feb 2013, Thomas Lumley wrote:> Is there some way to prevent finalizers running during a section of code? > > I have a package that includes R objects linked to database tables. To > maintain the call-by-value semantics, tables are copied rather than > modified, and the extra tables are removed by finalizers during garbage > collection. > > However, if the garbage collection occurs in the middle of processing > another SQL query (which is relatively likely, since that's where the > memory allocations are) there are problems with the database interface. > > Since the guarantees for the finalizer are "at most once, not before the > object is out of scope" it seems harmless to be able to prevent finalizers > from running during a particular code block, but I can't see any way to do > it. > > Suggestions? > > -thomas > > >-- Luke Tierney Chair, Statistics and Actuarial Science Ralph E. Wareham Professor of Mathematical Sciences University of Iowa Phone: 319-335-3386 Department of Statistics and Fax: 319-335-3017 Actuarial Science 241 Schaeffer Hall email: luke-tierney at uiowa.edu Iowa City, IA 52242 WWW: http://www.stat.uiowa.edu
I'm not sure I got your problem right, but you can keep a named copy of your not-to-be-finalized object as long as it needs to be around, so it doesn't go out of scope too early. Something like: local({ dontFinalizeMe <- obj ## ## code which creates copies, overwrites, and indirectly uses 'obj' ## }) hth, -- Antonio, Fabio Di Narzo, Biostatistician Mount Sinai School of Medicine, NY. 2013/2/12 Thomas Lumley <tlumley at uw.edu>:> Is there some way to prevent finalizers running during a section of code? > > I have a package that includes R objects linked to database tables. To > maintain the call-by-value semantics, tables are copied rather than > modified, and the extra tables are removed by finalizers during garbage > collection. > > However, if the garbage collection occurs in the middle of processing > another SQL query (which is relatively likely, since that's where the > memory allocations are) there are problems with the database interface. > > Since the guarantees for the finalizer are "at most once, not before the > object is out of scope" it seems harmless to be able to prevent finalizers > from running during a particular code block, but I can't see any way to do > it. > > Suggestions? > > -thomas > > > -- > Thomas Lumley > Professor of Biostatistics > University of Auckland > > [[alternative HTML version deleted]] > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel