Does free() actually get called? · Issue #20 · eddelbuettel/rcppgsl (original) (raw)

I can't figure out if the free member function or gsl_vector_free() is ever called in the destructors of the Vector or vector types - it looks to me like no? The quickest-ugliest check on this I could manage is to use a static that takes the first gsl_vector.data passed to the function - and then prints the first element of the static data out each time the function is called.

Rcpp::cppFunction("
void modify_vec_rcppgsl_frees(RcppGSL::Vector vec) {
    static int old_data_set = 0;
    static double * old_data;
    
    if (!old_data_set) {
        old_data = vec.data->data;
        old_data_set = 1;
    }

    Rcpp::Rcout << *old_data << \"\\n\";
    vec.free(); // or gsl_vector_free(vec)

}", depends="RcppGSL")

a <- 10
modify_vec_rcppgsl_frees(a) # 10
modify_vec_rcppgsl_frees(a) # 4.66318e-310

When free is explicitly called, the contents changes (unsure why the specific value is printed - must be a gsl thing). Now lets rely on the destructor instead;

 Rcpp::cppFunction("
void modify_vec_rcppgsl(RcppGSL::Vector vec) {
    static int old_data_set = 0;
    static double * old_data;
    
    if (!old_data_set) {
        old_data = vec.data->data;
        old_data_set = 1;
    }

    Rcpp::Rcout << *old_data << \"\\n\";
}", depends="RcppGSL")

modify_vec_rcppgsl(a) # 10
modify_vec_rcppgsl(a) # 10

Seems suspiciously like free() wasn't called?