Hi Bil,
I've stumbled upon something which seems like a bug in the gc.
I attach a snippet (which uses google test). I hope it is verbose enough
to demonstrate the problem.
To summarize, I definced a c_type to handle dynamic float arrays.
The test as it is attached passes. However the test will not pass if the
line proceeding the "problem 1" comment is uncommented.
The 2nd (minor) problem I stumbled upon is that one has to call "(gc)"
twice for the free methods to be called.
Another comment/question: how come there is no s7_run_gc function? From
what I can tell you can only trigger it from the scheme side with "(gc)"
/**
namespace aod {
namespace s7 {
typedef struct {
size_t size;
float* elements;
} float_arr;
}
}
**/
bool float_arr_gc__free_called = false;
void float_arr_gc__free(void* raw_data) {
float_arr_gc__free_called = true;
aod::s7::float_arr* data = (aod::s7::float_arr*) raw_data;
float* elements = data->elements;
delete[] elements;
delete data;
}
TEST ( c_primitives, float_arr_gc ) {
s7_scheme *sc = s7_init();
aod::s7::bind_primitives(sc);
s7_int float_arr_type = s7_number_to_integer(sc,
s7_eval_c_string(sc, "(*c-primitives* 'type-float-arr)"));
// overriding the free function to test if it's actually called
s7_c_type_set_free(sc, float_arr_type, float_arr_gc__free);
s7_pointer obj = s7_eval_c_string(sc,
"(define x (with-let *c-primitives* (float-arr 10 11)))");
aod::s7::float_arr *data = (aod::s7::float_arr*) s7_c_object_value(obj);
ASSERT_EQ(2, data->size);
ASSERT_EQ(10, data->elements[0]);
ASSERT_EQ(11, data->elements[1]);
/**
* Problem 1: Uncommenting the next line will cause the free method
* to not be called even when redefining the variable (thus variable no longer used)
**/
// s7_eval_c_string(sc, "(set! (x 0) 10)"); // this: if called, the free is not called
s7_eval_c_string(sc, "(define x 1)");
// the data should still be available, gc not called
ASSERT_EQ(10, data->elements[0]);
ASSERT_EQ(11, data->elements[1]);
/**
* Problem 2: Calling (gc) only once doesn't trigger the free method
* to be called
**/
// gc
// huh.. if only called once, doesn't work?
s7_eval_c_string(sc, "(gc)");
s7_eval_c_string(sc, "(gc)");
ASSERT_TRUE(float_arr_gc__free_called);
}
_______________________________________________
Cmdist mailing list
[email protected]
https://cm-mail.stanford.edu/mailman/listinfo/cmdist