-
Notifications
You must be signed in to change notification settings - Fork 53
Add test for external_pointer attribute preservation and fix move assignment operator #486
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b705fdf
Initial plan
Copilot fd0eb75
test: Add test for external_pointer attribute preservation on move (i…
Copilot b959f45
style: Apply clang-format to test-external_pointer.cpp
Copilot cc6f69a
fix: Prevent memory leaks in external_pointer test
Copilot 43d00f5
fix: Add missing return statement in move assignment operator
Copilot 5ec8642
docs: Final progress update
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| . |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -36,4 +36,53 @@ context("external_pointer-C++") { | |
| uniq.reset(); | ||
| expect_true(deleted == true); | ||
| } | ||
|
|
||
| test_that("external_pointer preserves attributes when moved (issue #308)") { | ||
| // Test move constructor | ||
| { | ||
| int* value = new int(42); | ||
| cpp11::external_pointer<int> p(value); | ||
|
|
||
| // Set an attribute on the external pointer | ||
| Rf_setAttrib(p, R_ClassSymbol, Rf_mkString("test_class")); | ||
|
|
||
| // Verify attribute exists before move | ||
| SEXP class_attr = Rf_getAttrib(p, R_ClassSymbol); | ||
| expect_true(class_attr != R_NilValue); | ||
|
|
||
| // Move the external pointer using move constructor | ||
| cpp11::external_pointer<int> p_moved = std::move(p); | ||
|
|
||
| // Verify attribute is preserved after move | ||
| SEXP class_attr_after = Rf_getAttrib(p_moved, R_ClassSymbol); | ||
| expect_true(class_attr_after != R_NilValue); | ||
| expect_true(strcmp(CHAR(STRING_ELT(class_attr_after, 0)), "test_class") == 0); | ||
|
|
||
| // Clean up | ||
| delete p_moved.release(); | ||
| } | ||
|
|
||
| // Test move assignment operator | ||
| { | ||
| int* value1 = new int(1); | ||
| cpp11::external_pointer<int> p1(value1); | ||
|
|
||
| // Set an attribute on p1 | ||
| Rf_setAttrib(p1, R_ClassSymbol, Rf_mkString("test_class")); | ||
|
|
||
| // Create p2 with nullptr (no memory leak) | ||
| cpp11::external_pointer<int> p2(nullptr); | ||
|
|
||
| // Move assign p1 to p2 | ||
| p2 = std::move(p1); | ||
|
|
||
| // Verify attribute is preserved after move assignment | ||
| SEXP class_attr_after = Rf_getAttrib(p2, R_ClassSymbol); | ||
| expect_true(class_attr_after != R_NilValue); | ||
| expect_true(strcmp(CHAR(STRING_ELT(class_attr_after, 0)), "test_class") == 0); | ||
|
|
||
| // Clean up | ||
| delete p2.release(); | ||
|
||
| } | ||
| } | ||
| } | ||
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Calling
deleteon the result ofrelease()will attempt to delete the memory that was already wrapped by the external pointer, but the external pointer's finalizer has already been set up to delete this memory when the R object is garbage collected. This creates a double-free scenario. Since the external pointer owns the memory, simply allowingp_movedto go out of scope is sufficient - the finalizer will handle cleanup.