Skip to content

Underlying CHARSXP in String class needs gc protection #321

@kevinushey

Description

@kevinushey

This code shows that CHARSXPs can be garbage collected:

#include <Rcpp.h>
using namespace Rcpp;

// [[Rcpp::export]]
NumericVector test() {
    Function gc("gc");

    SEXP character = Rf_mkChar("ouch");
    SEXP string = Rf_mkString("ouch");

    Shield<SEXP> other(Rf_allocVector(INTSXP, 1E6));
    gc();

    Rprintf("CHARSXP:\n");
    Rf_PrintValue(character);

    Rprintf("STRSXP:\n");
    Rf_PrintValue(string);

    return wrap(other);
}

/*** R
gctorture(TRUE)
invisible(test())
gctorture(FALSE)
*/
> Rcpp::sourceCpp('~/scratch/ouch.cpp')
> gctorture(TRUE)
> invisible(test())
CHARSXP:
[1] FALSE
STRSXP:
<garbage>
> gctorture(FALSE)

The CHARSXP has been replaced with garbage, as it has been GCed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions