[Rcpp-devel] ColDatum constructors memory safety

Dirk Eddelbuettel edd at debian.org
Fri Mar 19 03:49:44 CET 2010


Hi Alistair,

On 17 March 2010 at 06:55, Alistair Gee wrote:
| On Tue, Mar 16, 2010 at 5:49 PM, Dirk Eddelbuettel <edd at debian.org> wrote:
| >
| > On 16 March 2010 at 16:59, Alistair Gee wrote:
| > | Here are patches to RcppFrame.h:
| >
| > Sweet!  Thanks a lot!
| >
| > Now, just to top it off, could provide a unit test that would ideally trigger
| > the issue (or something related) pre-fix but not post-fix?
| >
| > Dirk
| 
| I'm not sure how to write a test for this since it is triggered by C++
| code (as opposed to R code), and the bug was only noticeable (aside
| from occasional segfaults) when I inspected the code. What do you
| suggest?
| 
| The code that I had that would trigger the problem was:
| 
|    vector<ColDatum> colDatumVector(10);

So using the home-grown ColDatum inside std::vector?  That works here:

test.ColDatum.vector <- function() {
    src <- 'std::vector<ColDatum> colDatumVector(10);
            return R_NilValue;';
    funx <- cfunction(signature(), src, Rcpp=TRUE)
    checkEquals(funx(), NULL, msg = "RcppColDatum.vector")
}


R> test.ColDatum.vector <- function() {
+     src <- 'std::vector<ColDatum> colDatumVector(10);
+             return R_NilValue;';
+     funx <- cfunction(signature(), src, Rcpp=TRUE)
+     checkEquals(funx(), NULL, msg = "RcppColDatum.vector")
+ }
R> library(RUnit)
R> test.ColDatum.vector()
[1] TRUE
R> 

That with current SVN sources but without your patch. 


So I became a little more ambitious and made the runit file a fuller test of
ColDatum and RcppFrame (both of the old API):

test.ColDatum.vector <- function() {
    src <- 'std::vector<ColDatum> colDatumVector(3);
            colDatumVector[0].setDoubleValue(1.23);
            colDatumVector[1].setIntValue(42);
            colDatumVector[2].setLogicalValue(0);
            std::vector<std::string> names;
            names.push_back("A");
            names.push_back("B");
            names.push_back("C");
            RcppFrame fr(names);
            fr.addRow(colDatumVector);
            RcppResultSet rs;
            rs.add("data.frame", fr);
            return R_NilValue;';
    funx <- cfunction(signature(), src, Rcpp=TRUE)
    checkEquals(funx(), NULL, msg = "RcppColDatum.vector")
}

(This defines sticks three different types of data in one vector of ColDatum
and then tries to insert this as a row into a RcppFrame.  I have used these
before, but not in a while.  IIRC this should have worked.  However:

R> funx()
Warning: stack imbalance in '.Call', 8 then 13
Warning: stack imbalance in '{', 6 then 11
NULL

and if I apply your patch, nothing changes and I get the same result (in a
new R session):

R> funx()
Warning: stack imbalance in '.Call', 8 then 13
Warning: stack imbalance in '{', 6 then 11
NULL
R> 

Can you provide a bit more background -- ideally in something like a unit
test?

Dirk

-- 
  Registration is open for the 2nd International conference R / Finance 2010
  See http://www.RinFinance.com for details, and see you in Chicago in April!


More information about the Rcpp-devel mailing list