From mrubi at fibertel.com.ar  Fri Feb 22 18:44:53 2008
From: mrubi at fibertel.com.ar (Martin Rubi)
Date: Fri, 22 Feb 2008 15:44:53 -0200
Subject: [Rd] Calling R_PreserveObject from embedded R
Message-ID: <000d01c8757a$a15a9ff0$0201a8c0@codedev>

Hello. This is my first post to the list, so first I'd like to thank 
everybody for making and mantaining such a great product as R.
I'm writting a native binding to R from Dolphin Smalltalk. I've followed up 
the examples of the documentation showing how to run R embedded, and I got 
it partially working. However, I have a problem with the reference handling 
of the R objects.
I've followed this strategy: every time I call a function in R and it 
answers me a SEXP, I called R_PreserveObject(sexp), and wrap it with a 
Smalltalk object. Whenever the Smalltalk object dies, I release the R object 
by calling R_ReleaseObject(sexp).
This seems to handle well the life cycle, but makes the running process to 
use a growing and a never ending amount of memory. Actually, after 
experimenting a while, I isolated the problem to iterate over a loop which 
all it does is create an expresion for a number, call PreserveObject and 
call ReleaseObject, and that alone makes the memory to grow indefinitely.
I couldn't find any comment about this behaviour. Is there something I'm 
missing ?
I'm running the embedded R-2.6.2 binaries for Windows in a WindowsXP sp2 
environment.

I've also implemented another strategy, which keeps a global list in R, and 
instead of calling R_PreserveObject, it inserts the SEXP in the list. This 
made the memory usage problem to go away, but the performance is noticeably 
worst compared with the other strategy, and is not as elegant as the first 
one neither, so I was hoping to be able to use the first strategy.

I'll appreciate any comment about what might be going on.
Thanks in advance.

Best regards.
martin



[Rd] Calling R_PreserveObject from embedded R
Martin Rubi