diff --git a/R/init.R b/R/init.R index 4c4c4488..b9d4c750 100644 --- a/R/init.R +++ b/R/init.R @@ -25,7 +25,5 @@ NULL } .onUnload = function(libname, pkgname) { - # force run weak finalizers before freeing sys - invisible(gc()) udunits_exit() } diff --git a/src/udunits.cpp b/src/udunits.cpp index ac980e82..58b0ae1d 100644 --- a/src/udunits.cpp +++ b/src/udunits.cpp @@ -30,10 +30,17 @@ typedef XPtr XPtrUT; static ut_system *sys = NULL; static ut_encoding enc = UT_UTF8; +// [[Rcpp::export]] +void udunits_exit() { + R_gc(); + ut_free_system(sys); + sys = NULL; +} + // [[Rcpp::export]] void udunits_init(CharacterVector path) { ut_set_error_message_handler(ut_ignore); - ut_free_system(sys); + udunits_exit(); for (int i = 0; i < path.size(); i++) { if ((sys = ut_read_xml(path[i])) != NULL) break; @@ -45,12 +52,6 @@ void udunits_init(CharacterVector path) { stop("no database found!"); // #nocov } -// [[Rcpp::export]] -void udunits_exit() { // #nocov start - ut_free_system(sys); - sys = NULL; -} // #nocov end - // wrap a ut_unit pointer in an XPtr SEXP ut_wrap(ut_unit *u) { XPtrUT p(u);