{"action":"created","issue":{"url":"https://api.github.com/repos/RcppCore/Rcpp/issues/248","labels_url":"https://api.github.com/repos/RcppCore/Rcpp/issues/248/labels{/name}","comments_url":"https://api.github.com/repos/RcppCore/Rcpp/issues/248/comments","events_url":"https://api.github.com/repos/RcppCore/Rcpp/issues/248/events","html_url":"https://github.com/RcppCore/Rcpp/pull/248","id":56410018,"number":248,"title":"Simplify generated attributes code for RNGScope","user":{"login":"jjallaire","id":104391,"avatar_url":"https://avatars.githubusercontent.com/u/104391?v=3","gravatar_id":"","url":"https://api.github.com/users/jjallaire","html_url":"https://github.com/jjallaire","followers_url":"https://api.github.com/users/jjallaire/followers","following_url":"https://api.github.com/users/jjallaire/following{/other_user}","gists_url":"https://api.github.com/users/jjallaire/gists{/gist_id}","starred_url":"https://api.github.com/users/jjallaire/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jjallaire/subscriptions","organizations_url":"https://api.github.com/users/jjallaire/orgs","repos_url":"https://api.github.com/users/jjallaire/repos","events_url":"https://api.github.com/users/jjallaire/events{/privacy}","received_events_url":"https://api.github.com/users/jjallaire/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2015-02-03T17:33:23Z","updated_at":"2015-02-03T19:42:54Z","closed_at":null,"pull_request":{"url":"https://api.github.com/repos/RcppCore/Rcpp/pulls/248","html_url":"https://github.com/RcppCore/Rcpp/pull/248","diff_url":"https://github.com/RcppCore/Rcpp/pull/248.diff","patch_url":"https://github.com/RcppCore/Rcpp/pull/248.patch"},"body":"Use RObject and it's destructor rather than SEXP protect/unprotect. The code we were generating was correct however hard to read and reason about for users not familiar with R/Rcpp internals. Previously we held the result in a raw SEXP which we unprotected after leaving a special block created for the RNGScope:\r\n\r\n```cpp\r\nRcppExport SEXP rcpp_hello_world() {\r\nBEGIN_RCPP\r\n    SEXP __sexp_result;\r\n    {\r\n        Rcpp::RNGScope __rngScope;\r\n        List __result = rcpp_hello_world();\r\n        PROTECT(__sexp_result = Rcpp::wrap(__result));\r\n    }\r\n    UNPROTECT(1);\r\n    return __sexp_result;\r\nEND_RCPP\r\n}\r\n```\r\n\r\nNow, we simply construct an RObject *prior to* the RNGScope on the stack and then return that object (this ensures that any GC triggered by PutRNGState won't collect the result SEXP before it can be returned):\r\n\r\n```cpp\r\nRcppExport SEXP rcpp_hello_world() {\r\nBEGIN_RCPP\r\n    Rcpp::RObject __result;\r\n    Rcpp::RNGScope __rngScope;\r\n    __result = Rcpp::wrap(rcpp_hello_world());\r\n    return __result;\r\nEND_RCPP\r\n}\r\n```\r\n"},"comment":{"url":"https://api.github.com/repos/RcppCore/Rcpp/issues/comments/72719692","html_url":"https://github.com/RcppCore/Rcpp/pull/248#issuecomment-72719692","issue_url":"https://api.github.com/repos/RcppCore/Rcpp/issues/248","id":72719692,"user":{"login":"jjallaire","id":104391,"avatar_url":"https://avatars.githubusercontent.com/u/104391?v=3","gravatar_id":"","url":"https://api.github.com/users/jjallaire","html_url":"https://github.com/jjallaire","followers_url":"https://api.github.com/users/jjallaire/followers","following_url":"https://api.github.com/users/jjallaire/following{/other_user}","gists_url":"https://api.github.com/users/jjallaire/gists{/gist_id}","starred_url":"https://api.github.com/users/jjallaire/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jjallaire/subscriptions","organizations_url":"https://api.github.com/users/jjallaire/orgs","repos_url":"https://api.github.com/users/jjallaire/repos","events_url":"https://api.github.com/users/jjallaire/events{/privacy}","received_events_url":"https://api.github.com/users/jjallaire/received_events","type":"User","site_admin":false},"created_at":"2015-02-03T19:42:54Z","updated_at":"2015-02-03T19:42:54Z","body":"FYI rebuilt the entire Rcpp Gallery with this change and all is well (probably also worth a revdep check). I think this is a straightforward change however I could use a sanity check to make sure I'm not missing something (as the code being modified was put in there originally to avoid a crash under extreme GC conditions and I'd certainly hate to regress back to that behavior). @eddelbuettel @kevinushey @romainfrancois "}}