External pointers and attributes

Open
#308 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
50/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
cpp, r
Domain
backend

Research direction

Start with cpp11/external_pointer.hpp and the R reproduction in the issue; compare the vendored cpp11 implementation with the referenced DuckDB commit. Confirm that moving an external pointer preserves its attributes, then rerun the supplied example and verify the third and fourth outputs retain the class attribute.

Written by the indexing model from the issue text.

Description

Invoking the move constructor of external_pointer<> clears attributes on an external pointer object.

I would expect the third and fourth output to be identical to the second output. A possible fix is https://github.com/duckdb/duckdb/pull/6526/commits/12f6f10c12edda4d1153777d7a172279be11191c (this project uses a vendored version of cpp11).

library(cpp11)

cpp_source(
  code = '
  #include "cpp11/external_pointer.hpp"
  #include "cpp11/strings.hpp"

  template <class X>
  cpp11::external_pointer<int> test_int() {
    X a = 0;
    auto p = cpp11::external_pointer<X>(&a, false);
    Rf_PrintValue(p);
    Rf_setAttrib(p, R_ClassSymbol, cpp11::writable::strings({"foo"}));
    Rf_PrintValue(p);

    return std::move(p);
  }

  [[cpp11::register]]
  SEXP test() {
    auto p = cpp11::sexp(test_int<int>());
    Rf_PrintValue(p);
    return p;
  }
  '
)

out <- test()
#> <pointer: 0x16f0af42c>
#> <pointer: 0x16f0af42c>
#> attr(,"class")
#> [1] "foo"
#> <pointer: 0x16f0af42c>
out
#> <pointer: 0x16f0af42c>

Created on 2023-03-02 with reprex v2.0.2

Dominant language
C++
Stars
224
Forks
52
PR merge metrics
No merged PRs in 30d

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from r-lib/cpp11

All issues in r-lib/cpp11

Similar issues

More C++ issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.