Skip to content

Commit

Permalink
always force run weak finalizers before freeing sys
Browse files Browse the repository at this point in the history
  • Loading branch information
Enchufa2 committed Mar 11, 2021
1 parent d72c0e4 commit 3e451b3
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 9 deletions.
2 changes: 0 additions & 2 deletions R/init.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,5 @@ NULL
}

.onUnload = function(libname, pkgname) {
# force run weak finalizers before freeing sys
invisible(gc())
udunits_exit()
}
15 changes: 8 additions & 7 deletions src/udunits.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,17 @@ typedef XPtr<ut_unit, PreserveStorage, ut_free, true> 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;
Expand All @@ -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);
Expand Down

0 comments on commit 3e451b3

Please sign in to comment.