r-lib / R6

Encapsulated object-oriented programming for R
https://R6.r-lib.org
Other
409 stars 56 forks source link

finalize() not triggered on rm() ? #283

Closed moodymudskipper closed 8 months ago

moodymudskipper commented 8 months ago

I was not sure whether to post this in the Advanced R repo or here, sorry if I chose wrong. In advanced R we find the following example in 14.4.2, however in the book rm() triggers the message, below in my reprex it doesn't.

library(R6)
TemporaryFile <- R6Class("TemporaryFile", list(
  path = NULL,
  initialize = function() {
    self$path <- tempfile()
  },
  finalize = function() {
    message("Cleaning up ", self$path)
    unlink(self$path)
  }
))
tf <- TemporaryFile$new()
rm(tf)

Created on 2024-01-23 with reprex v2.0.2

Session info ``` r sessioninfo::session_info() #> ─ Session info ─────────────────────────────────────────────────────────────── #> setting value #> version R version 4.2.1 (2022-06-23) #> os macOS Ventura 13.3.1 #> system aarch64, darwin20 #> ui X11 #> language (EN) #> collate en_US.UTF-8 #> ctype en_US.UTF-8 #> tz Europe/Zurich #> date 2024-01-23 #> pandoc 3.1.1 @ /Applications/RStudio.app/Contents/Resources/app/quarto/bin/tools/ (via rmarkdown) #> #> ─ Packages ─────────────────────────────────────────────────────────────────── #> package * version date (UTC) lib source #> cli 3.6.1 2023-03-23 [1] CRAN (R 4.2.0) #> digest 0.6.33 2023-07-07 [1] CRAN (R 4.2.0) #> evaluate 0.23 2023-11-01 [1] CRAN (R 4.2.0) #> fastmap 1.1.1 2023-02-24 [1] CRAN (R 4.2.0) #> fs 1.6.3 2023-07-20 [1] CRAN (R 4.2.0) #> glue 1.6.2 2022-02-24 [1] CRAN (R 4.2.0) #> htmltools 0.5.7 2023-11-03 [1] CRAN (R 4.2.0) #> knitr 1.45 2023-10-30 [1] CRAN (R 4.2.0) #> lifecycle 1.0.4 2023-11-07 [1] CRAN (R 4.2.1) #> magrittr 2.0.3 2022-03-30 [1] CRAN (R 4.2.0) #> purrr 1.0.2 2023-08-10 [1] CRAN (R 4.2.0) #> R.cache 0.16.0 2022-07-21 [1] CRAN (R 4.1.1) #> R.methodsS3 1.8.2 2022-06-13 [1] CRAN (R 4.1.1) #> R.oo 1.25.0 2022-06-12 [1] CRAN (R 4.1.1) #> R.utils 2.12.2 2022-11-11 [1] CRAN (R 4.2.0) #> R6 * 2.5.1 2021-08-19 [1] CRAN (R 4.2.0) #> reprex 2.0.2 2022-08-17 [1] CRAN (R 4.2.0) #> rlang 1.1.2 2023-11-04 [1] CRAN (R 4.2.1) #> rmarkdown 2.25 2023-09-18 [1] CRAN (R 4.2.0) #> rstudioapi 0.15.0 2023-07-07 [1] CRAN (R 4.2.0) #> sessioninfo 1.2.2 2021-12-06 [1] CRAN (R 4.2.0) #> styler 1.10.2 2023-11-07 [1] Github (r-lib/styler@c38d7f9) #> vctrs 0.6.4 2023-10-12 [1] CRAN (R 4.2.0) #> withr 2.5.2 2023-10-30 [1] CRAN (R 4.2.0) #> xfun 0.41 2023-11-01 [1] CRAN (R 4.2.0) #> yaml 2.3.7 2023-01-23 [1] CRAN (R 4.2.0) #> #> [1] /Users/Antoine/Library/R/arm64/4.2/library #> [2] /opt/R/4.2.1-arm64/Resources/library #> #> ────────────────────────────────────────────────────────────────────────────── ```

I don't find where the doc for finalize() sits in the R6 package and I don't know how hooks on rm() or garbage collection would work so I don't know how to dig further.

cc @hadley

hadley commented 8 months ago

It's finalised on gc()