Repository navigation
document ownership of objects returned to r from modules - #1534
Open
kevinushey wants to merge 1 commit into
Open
kevinushey wants to merge 1 commit into
kevinushey wants to merge 1 commit into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1533.
A raw pointer (
T*) returned from a module function or method is wrapped in a new R object that owns it (make_new_object()createsXPtr<T>(ptr, true)), so the object is deleted when that R object is collected. That is what factory-style code returningnew T(...)relies on, so this PR doesn't change it. But it wasn't documented, and returningthis(to chain calls), a pointer to a member or container element, or the same pointer twice deletes the object more than once. See #1533 for the reprex.Changes:
vignettes/rmd/Rcpp-modules.Rmd: new subsection "Returning objects of exposed classes", after "ExtendingRcpp::asandRcpp::wrap". It explains that objects returned by value or by reference are copied and owned by R, while a returned pointer is owned and eventually deleted by R, so it may only be returned when nothing else owns the object. It lists the patterns that lead to double deletes, with a short example. Only the Rmd source changes; the PDF can be regenerated with the other vignettes at release time.inst/tinytest/cpp/Module.cpp,inst/tinytest/test_module.R: aModuleOwnedclass that counts its destructions, and a test that a pointer returned fromModuleOwned_create()is deleted once the R object wrapping it is garbage collected. This pins down the documented behavior. It passes on master too, since nothing changes in the code.Verification:
T&, or a free function returningT&, leaves the original untouched.test_module.R(47/47) passes withRunAllRcppTests=yes(R 4.6.1, macOS).Checklist
R CMD checkstill passes all tests (test_module.Rrun locally; documentation-only otherwise)