Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions cpp11test/src/test-external_pointer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
}
11 changes: 9 additions & 2 deletions inst/include/cpp11/external_pointer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,16 @@ class external_pointer {
data_ = safe[Rf_shallow_duplicate](rhs.data_);
}

external_pointer(external_pointer&& rhs) { reset(rhs.release()); }
external_pointer(external_pointer&& rhs) {
data_ = rhs.data_;
rhs.data_ = R_NilValue;
}

external_pointer& operator=(external_pointer&& rhs) noexcept { reset(rhs.release()); }
external_pointer& operator=(external_pointer&& rhs) noexcept {
data_ = rhs.data_;
rhs.data_ = R_NilValue;
Comment on lines +77 to +79
Copy link

Copilot AI Jan 22, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The move assignment operator overwrites data_ without releasing the previously held resource, potentially causing a memory leak. The old data_ should be released before assigning the new value.

Suggested change
data_ = rhs.data_;
rhs.data_ = R_NilValue;
if (this != &rhs) {
if (data_ != R_NilValue) {
r_deleter(data_);
}
data_ = rhs.data_;
rhs.data_ = R_NilValue;
}

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is wrong and defeats the purpose of this PR. Added a clarification comment.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had no idea about this copilot thing. I will take a look!

Btw, in the cpp4r I ended up doing things a bit different https://github.com/pachadotdev/cpp4r/blob/main/inst/include/cpp4r/external_pointer.hpp

return *this;
}
Comment on lines +74 to +81
Copy link

Copilot AI Jan 22, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The move assignment operator does not handle self-assignment correctly. If this == &rhs, the operation will set data_ to R_NilValue before returning, resulting in a null pointer. Add a self-assignment check at the beginning of the function.

Copilot uses AI. Check for mistakes.

external_pointer& operator=(std::nullptr_t) noexcept { reset(); };

Expand Down