[Rcpp-devel] Life-cycle of Rcpp::XPtr

Simon Urbanek simon.urbanek at R-project.org
Sat Sep 23 11:49:18 CEST 2023


The copy constructor doesn't actually create a copy, it only acts as a wrapper that preserves the same EXTPTR object, it is akin to increasing the reference count, so the C++ class wrapped in the EXTPTR is only released when the EXTPTR can be garbage-collected, i.e. all references are gone (including all "copies").
[Of course that is not true if you were to create two XPtrs with the same pointer and a finalizer each, but that would be a bad idea, obviously].

Cheers,
Simon


> On Sep 23, 2023, at 8:28 PM, Ralf Stubner <ralf.stubner at gmail.com> wrote:
> 
> 
> Hi everybody,
> 
> I have a question concerning the file-cycle of Rcpp::XPtr: Consider a
> XPtr with the default delete finalizer wrapping some pointer. If I use
> the copy constructor to create another XPtr, this is pointing at the
> same underlying object as expected. What happens if one of these
> pointers goes out of scope and is at some point garbage collected? Is
> the underlying object deleted leaving the other XPtr with a broken
> pointer? Or is the object protected by the existence of the other
> pointer? From my experiments I have the impression that the latter is
> the case, which would be the desired behaviour. But it would be nice
> if one could be sure.
> 
> Thanks
> Ralf
> _______________________________________________
> Rcpp-devel mailing list
> Rcpp-devel at lists.r-forge.r-project.org
> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
> 



More information about the Rcpp-devel mailing list