Skip to content
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

Running XPtr finalizer on exit #655

Closed
jeroen opened this issue Mar 15, 2017 · 3 comments
Closed

Running XPtr finalizer on exit #655

jeroen opened this issue Mar 15, 2017 · 3 comments

Comments

@jeroen
Copy link
Contributor

jeroen commented Mar 15, 2017

[I tried posting on the mailing list but having issues with my UCLA email...]

I am experiencing odd behavior in packages that use XPtr (V8, tesseract, xml2) which is caused by the fact that Rcpp does not run the finalizer when R quits. This can lead to corrupted connections, memory leaks, etc.

The problem is that Rcpp hardcodes the call to R_RegisterCFinalizerEx with onexit = FALSE which makes that R skips over the finalizer when exiting.

Currently there is no way to fix this on the package side. Would there be a way to expose onexit as an option in Rcpp? Or better: make the default TRUE (imo you should always cleanup after yourself). But I can live with opt-in if there is a good reason for not finalizing.

@eddelbuettel
Copy link
Member

I tried posting on the mailing list but having issues with my UCLA email..

You may need to resubscribe from the Berkeley address (or gmail or ...). Mailman uses strict string equality.

@eddelbuettel
Copy link
Member

The lifetime management is a delicate issue. In some (smaller, simpler) projects I just brought a general instance of what I needed up, and then down explicitly at the end. You may well deal with way more complicated setups here.

But the onexit=FALSE you identify may indeed help. We could put the boolean into the function signature of setDeleteFinalizer() with a default of FALSE, thus allowing the override. Can you try that at your end and see if it helps?

@jeroen
Copy link
Contributor Author

jeroen commented Mar 15, 2017

I think ideally we would add a fourth argument to the XPtr template to be consistent with R's C API.

So the example from xml2 is now:

typedef Rcpp::XPtr<xmlDoc,Rcpp::PreserveStorage,xmlFreeDoc> XPtrDoc;

And then to enable finalizing on exit one would do:

typedef Rcpp::XPtr<xmlDoc,Rcpp::PreserveStorage,xmlFreeDoc, true> XPtrDoc;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants