[Rcpp-devel] Rcpp and ExternalPtr

Dirk Eddelbuettel edd at debian.org
Sun Dec 7 11:52:57 CET 2014


On 6 December 2014 at 21:41, Jeroen Ooms wrote:
| On Fri, Dec 5, 2014 at 12:54 AM, Romain François
| <romain at r-enthusiasts.com> wrote:
| >
| > You are looking for XPtr.
| 
| Thanks guys, exactly what I needed indeed. One follow-up question:
| 
| Is there a way to use the XPtr object to prevent the c++ object it
| points to from going out of scope? It seems like the c++ garbage
| collector destroys the handle after the function returns, even though
| the handle is referenced by the XPtr that I just created. I was hoping
| that it would automatically be protected as long as the EXTPTRSXP is
| pointing to it, but that does not seem to be the case.
| 
| I am currently working around it by maintaining a list of handles in
| the global scope, but it would be more elegant if R(cpp) would take
| care of it.

What you are after, if I understand it correctly, is (close to) a common and
well studied idiom called 'RAII': Resource Acquisition is Initialization. Two
quick links, and you will find many more:

http://en.wikibooks.org/wiki/More_C++_Idioms/Resource_Acquisition_Is_Initialization

http://programmers.stackexchange.com/questions/29692/whats-raii-examples

Dirk

-- 
http://dirk.eddelbuettel.com | @eddelbuettel | edd at debian.org


More information about the Rcpp-devel mailing list