Saptarshi Guha
2014-Mar-06 22:32 UTC
[Rd] A question about multiple(?) out of order ReleaseObject
Hello, This is a question that probably reveals my lack of understanding. In a C function (call it cfunc), i created a SEXP, called S, and then called R_PreserveObject on S. I returned the SEXP to the calling R function (call it rfunc). Note, I didn't call R_ReleaseObject on S. v <- .Call("cfunc") So, are the following statements correct 1. S is 'doubly' protected from the GC by being associated both with 'v' and because it has been added to the precious list (via a call to R_PreserveObject without ReleaseObject being called) 2. I have another C function called cfunc2. In cfunc2, I call R_ReleaseObject on S. S , however, is still protected from the GC, because it is associated with 'v' Is (1) and (2) correct? I have not used R_protect/unprotect, because if I return from cfunc without the equivalent number of unprotects, i get 'unbalanced stack' warnings. I'd rather not have to worry about that because i intend to balance it later. Regards Saptarshi [[alternative HTML version deleted]]
Gabriel Becker
2014-Mar-06 22:48 UTC
[Rd] A question about multiple(?) out of order ReleaseObject
Saptarshi, R_PreserveObject and R_ReleaseObject are, as far as I know, intended for the rare situations where you need to maintain a SEXP in C that is not pointed to by any R level symbols, and across e.g. .Calls. If you are returning an object to R ("v" in this case) and intend to do .Call("cfunc2", v) later, there is no benefit at all to having called R_PreserveObject on it. The only case where your object would lose protection (v and all other R symbols being rm'd/going out of scope) would also cause to to lose your only reference to the SEXP, so by R_Preserve'ing all you will have done is create an unreachable protected pointer. If you are keeping a static pointer to the SEXP down in C code that R can't see, then R_PreserveObject would be appropriate, but the situations where doing that is a good idea are rare (though they do exist). HTH, ~G On Thu, Mar 6, 2014 at 2:32 PM, Saptarshi Guha <saptarshi.guha@gmail.com>wrote:> Hello, > > This is a question that probably reveals my lack of understanding. > In a C function (call it cfunc), i created a SEXP, called S, and then > called R_PreserveObject on S. > > I returned the SEXP to the calling R function (call it rfunc). Note, I > didn't call > R_ReleaseObject on S. > > v <- .Call("cfunc") > > So, are the following statements correct > > 1. S is 'doubly' protected from the GC by being associated both with 'v' > and because it has been added to the precious list (via a call to > R_PreserveObject without ReleaseObject being called) > > 2. I have another C function called cfunc2. In cfunc2, I call > R_ReleaseObject on S. S , however, is still protected from the GC, because > it is associated with 'v' > > Is (1) and (2) correct? > > I have not used R_protect/unprotect, because if I return from cfunc without > the equivalent number of unprotects, i get 'unbalanced stack' warnings. I'd > rather not have to worry about that because i intend to balance it later. > > Regards > Saptarshi > > [[alternative HTML version deleted]] > > ______________________________________________ > R-devel@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel >-- Gabriel Becker Graduate Student Statistics Department University of California, Davis [[alternative HTML version deleted]]
Simon Urbanek
2014-Mar-07 03:05 UTC
[Rd] A question about multiple(?) out of order ReleaseObject
On Mar 6, 2014, at 5:32 PM, Saptarshi Guha <saptarshi.guha at gmail.com> wrote:> Hello, > > This is a question that probably reveals my lack of understanding. > In a C function (call it cfunc), i created a SEXP, called S, and then > called R_PreserveObject on S. > > I returned the SEXP to the calling R function (call it rfunc). Note, I > didn't call > R_ReleaseObject on S. > > v <- .Call("cfunc") > > So, are the following statements correct > > 1. S is 'doubly' protected from the GC by being associated both with 'v' > and because it has been added to the precious list (via a call to > R_PreserveObject without ReleaseObject being called) >yes> 2. I have another C function called cfunc2. In cfunc2, I call > R_ReleaseObject on S. S , however, is still protected from the GC, because > it is associated with 'v' >yes (assuming the binding to v still exists at that point). Note, however, that is such a case you R_PreserveObject() is pointless since you don't need to protect it on exit (that's in fact the convention - return results are never protected).> Is (1) and (2) correct? > > I have not used R_protect/unprotect, because if I return from cfunc without the equivalent number of unprotects, i get 'unbalanced stack' warnings. I'd rather not have to worry about that because i intend to balance it later. >Normally, you should not keep the result of a function protected since it means you *have* to guarantee the unprotect at a later point. That is in general impossible to guarantee unless you have another object that is holding a reference that will be cleared by an explicitly registered finalizer. So unless that is the case, you are creating an explicit leak = bad. If you don't have as stack-like design, you can always use explicitly managed object for the lifetime (personally, I prefer that) since all chained objects are protected by design, or use REPROTECT. Cheers, Simon> Regards > Saptarshi > > [[alternative HTML version deleted]] > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel >
Apparently Analagous Threads
- Repost: (apologies for HTML post) A question about multiple(?) out of order ReleaseObject
- Many apologies: last post: A question about multiple(?) out of order ReleaseObject
- Calling R_PreserveObject from embedded R
- R-devel Digest, Vol 83, Issue 2
- calling R API functions after engine shutdown