-
Notifications
You must be signed in to change notification settings - Fork 158
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Pipe messing with the environment #146
Comments
Also note that it works correctly with a named function: func <- function() 1
foo <- Foo__new() %>%
Foo__add(func)
rm(foo)
gc()
# Warning: foo destructor called I'm unable to replicate this with R6 classes or |
In this call:
The parent environment of the function created through
rather than
My guess is that this ultimately comes down to something going wrong with how |
Moreover: Browse[2]> ls(envir = environment(fun), all.names=TRUE)
[1] "."
Browse[2]> environment(fun)$.
<pointer: 0x3369e20>
Browse[2]> foo_
<pointer: 0x3369e20> There it is the other copy of the pointer! That's why the destructor is not being called. Thanks, @kevinushey, for your help. |
Is it the same issue with the |
Just checked and yes, same issue. |
Hmm it's not clear to me what's going on - one of the Rcpp experts may know... @romainfrancois ? |
The |
Seems to be an unhappy intersection of @Enchufa2 having code that would really like the object to be destroyed so that Sometimes one can fix the data type to add an explicit 'I am done, please unwind' member function to manually do some of the work of the destructor (at the cost of some local code complexity) -- but I am not sure one could do that here with Modules. |
Yes, basically, I have users' reports about machines running out of memory after many replications of a simulation model with simmer. I recommend them to reset the simulation environment, calling explicitly a function that deallocates resources, but I cannot rely on that. Nevertheless, knowing what's the problem, I think I can implement in my package a workaround to transparently remove the reference that magrittr creates until this issue is solved. |
Ok, I finally managed to find an example not involving Rcpp (sorry, guys, and thanks for your help). Consider the following not-so-useful function: store <- function(env1, env2, func) {
env2$func <- func
env1
} Then, as expected, a <- new.env()
b <- new.env()
reg.finalizer(a, function(e) print("deleting 'a'"))
a <- store(a, b, function() 1)
rm(a); gc()
# deleting 'a'
rm(b); gc() but library(magrittr)
a <- new.env()
b <- new.env()
reg.finalizer(a, function(e) print("deleting 'a'"))
a <- a %>% store(b, function() 1)
rm(a); gc()
rm(b); gc()
# deleting 'a' That's not ok. Finally, a workaround: library(magrittr)
a <- new.env()
b <- new.env()
reg.finalizer(a, function(e) print("deleting 'a'"))
a <- a %>% store(b, function() 1)
rm(".", envir=environment(b$func)) # the trick
rm(a); gc()
# deleting 'a'
rm(b); gc() That's ok again. |
Sorry in advance because the minimal reprex is not that minimal: it involves
Rcpp
, and the issue I found is that some objects are not being garbage-collected because ofmagrittr
(I'm adding @eddelbuettel to the loop, just in case I'm mistaken and this has something to do withRcpp
instead). Let's consider the following mini-module:There is a class
Foo
that stores pointers toBar
, andBar
stores an R function, which is received throughFoo::add
. This code can be compiled withRcpp::sourceCpp(code='<code_here>')
. Then, let's play around:Foo
's destructor is correctly called and we see the warning. Perfect. But now,No warning! This is very serious, because the memory is not being deallocated. My guess is that
magrittr
could be messing with the environments and there is still a reference tofoo
somewhere, hidden, so that R is not removing the object. Is it possible?The text was updated successfully, but these errors were encountered: