Skip to content

Conversation

@krlmlr
Copy link
Member

@krlmlr krlmlr commented Dec 7, 2024

From duckdb, see #401.

Fixes #308.

This is fairly severe.

@krlmlr krlmlr requested a review from DavisVaughan December 7, 2024 19:48
krlmlr added a commit to duckdb/duckdb-r that referenced this pull request Dec 7, 2024
r-lib/cpp11#423

This reverts commit 1b698e533ea7c7003cb610a5f5f7e5a47966c59d.
krlmlr added a commit to duckdb/duckdb-r that referenced this pull request Dec 7, 2024
* Revert "ifndef"

This reverts commit 9afb676.

* Revert "Reapply 782630b minus the whitespace changes"

This reverts commit f05d56c.

* Reapply "Revert a011d42"

This reverts commit fe615c4.

* Reapply "Revert fb18a2d"

This reverts commit 6f4979d.

* Vendor cpp11 0.5.1

* Add `prot` argument to `external_pointer()` constructor

r-lib/cpp11#420

This reverts commit abcae40e2aff8b1664098294959b8df743605737.

* END_CPP11_EX()

r-lib/cpp11#421

* Protection

r-lib/cpp11#422

This reverts commit 1e181e5e5c2cf0a21906b49dafe80364c8ca3f2b.

* External pointer premature release

r-lib/cpp11#423

This reverts commit 1b698e533ea7c7003cb610a5f5f7e5a47966c59d.
krlmlr added a commit to duckdb/duckdb-r that referenced this pull request Dec 8, 2024
* Revert "ifndef"

This reverts commit 9afb676.

* Revert "Reapply 782630b minus the whitespace changes"

This reverts commit f05d56c.

* Reapply "Revert a011d42"

This reverts commit fe615c4.

* Reapply "Revert fb18a2d"

This reverts commit 6f4979d.

* Vendor cpp11 0.5.1

* Add `prot` argument to `external_pointer()` constructor

r-lib/cpp11#420

This reverts commit abcae40e2aff8b1664098294959b8df743605737.

* END_CPP11_EX()

r-lib/cpp11#421

* Protection

r-lib/cpp11#422

This reverts commit 1e181e5e5c2cf0a21906b49dafe80364c8ca3f2b.

* External pointer premature release

r-lib/cpp11#423

This reverts commit 1b698e533ea7c7003cb610a5f5f7e5a47966c59d.
krlmlr added a commit to duckdb/duckdb-r that referenced this pull request Dec 8, 2024
* Revert "ifndef"

This reverts commit 9afb676.

* Revert "Reapply 782630b minus the whitespace changes"

This reverts commit f05d56c.

* Reapply "Revert a011d42"

This reverts commit fe615c4.

* Reapply "Revert fb18a2d"

This reverts commit 6f4979d.

* Vendor cpp11 0.5.1

* Add `prot` argument to `external_pointer()` constructor

r-lib/cpp11#420

This reverts commit abcae40e2aff8b1664098294959b8df743605737.

* END_CPP11_EX()

r-lib/cpp11#421

* Protection

r-lib/cpp11#422

This reverts commit 1e181e5e5c2cf0a21906b49dafe80364c8ca3f2b.

* External pointer premature release

r-lib/cpp11#423

This reverts commit 1b698e533ea7c7003cb610a5f5f7e5a47966c59d.
@pachadotdev
Copy link
Contributor

From duckdb, see #401.

This is fairly severe.

I found the same while porting magick

krlmlr added a commit to duckdb/duckdb-r that referenced this pull request Mar 9, 2025
r-lib/cpp11#423

This reverts commit 1b698e533ea7c7003cb610a5f5f7e5a47966c59d.
krlmlr added a commit to duckdb/duckdb-r that referenced this pull request Mar 9, 2025
* Revert "External pointer premature release"

This reverts commit d7886e3.

* Revert "Protection"

This reverts commit 4f4a3a8.

* Revert "END_CPP11_EX()"

This reverts commit aa3225d.

* Revert "Add `prot` argument to `external_pointer()` constructor"

This reverts commit d045ccf.

* Header

* Substance

* Add `prot` argument to `external_pointer()` constructor

r-lib/cpp11#420

This reverts commit abcae40e2aff8b1664098294959b8df743605737.

* END_CPP11_EX()

r-lib/cpp11#421

* Protection

r-lib/cpp11#422

This reverts commit 1e181e5e5c2cf0a21906b49dafe80364c8ca3f2b.

* External pointer premature release

r-lib/cpp11#423

This reverts commit 1b698e533ea7c7003cb610a5f5f7e5a47966c59d.
pachadotdev added a commit to pachadotdev/cpp4r that referenced this pull request Oct 1, 2025
@krlmlr
Copy link
Member Author

krlmlr commented Oct 2, 2025

@copilot: Add a test based on the linked issue #308.

@krlmlr
Copy link
Member Author

krlmlr commented Oct 2, 2025

I imagine that this would need a test. Copilot does not talk to me today, but I remember testing with the motivating example in #308, and the vendored and patched version of cpp11 works just fine for me.

@pachadotdev
Copy link
Contributor

I imagine that this would need a test. Copilot does not talk to me today, but I remember testing with the motivating example in #308, and the vendored and patched version of cpp11 works just fine for me.

hi!
I ended up creating cpp4r to add some corrections/features that are out scope based on the last email from Hadley

@pachadotdev
Copy link
Contributor

I imagine that this would need a test. Copilot does not talk to me today, but I remember testing with the motivating example in #308, and the vendored and patched version of cpp11 works just fine for me.

and you can vendor to src, I tagged you here pachadotdev/cpp4r@11afe2f

@krlmlr
Copy link
Member Author

krlmlr commented Jan 22, 2026

@copilot: Add a test based on the linked issue #308.

Copy link

Copilot AI commented Jan 22, 2026

@krlmlr I've opened a new pull request, #486, to work on those changes. Once the pull request is ready, I'll request review from you.

…ignment operator (#486)

Co-authored-by: krlmlr <1741643+krlmlr@users.noreply.github.com>
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings January 22, 2026 06:05
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes a critical bug in the external_pointer class where move operations were causing premature release of external pointers, potentially leading to data corruption or crashes. The fix ensures that attributes attached to external pointers are preserved during move operations.

Changes:

  • Modified move constructor and move assignment operator to transfer ownership without calling reset() and release()
  • Added comprehensive test coverage for verifying attribute preservation during move operations

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
inst/include/cpp11/external_pointer.hpp Fixed move constructor and move assignment operator to preserve pointer attributes by directly transferring data_ instead of using reset(release())
cpp11test/src/test-external_pointer.cpp Added test cases to verify that external pointer attributes are preserved during move constructor and move assignment operations

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 74 to 78
external_pointer& operator=(external_pointer&& rhs) noexcept {
data_ = rhs.data_;
rhs.data_ = R_NilValue;
return *this;
}
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.
Comment on lines 75 to 76
data_ = rhs.data_;
rhs.data_ = R_NilValue;
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
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.

@krlmlr
Copy link
Member Author

krlmlr commented Jan 22, 2026

Still to be done: run tests with existing cpp11 code to see if they fail.

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

Successfully merging this pull request may close these issues.

External pointers and attributes

3 participants