#50 - more memory control
authorFedor Yudanov <fedwiz@academ.org>
Tue, 9 Apr 2013 10:11:40 +0000 (17:11 +0700)
committerFedor Yudanov <fedwiz@academ.org>
Tue, 9 Apr 2013 10:11:40 +0000 (17:11 +0700)
rbejdb/src/rbbson.c
rbejdb/src/rbejdb.c

index 013df03..c41f9c9 100644 (file)
@@ -43,12 +43,20 @@ void init_ruby_to_bson() {
     bsonWrapClass = rb_define_class(BSON_RUBY_CLASS, rb_cObject);
 }
 
+void rbbson_context_free(RBBSON_CONTEXT* rbbsctx) {
+    ruby_xfree(rbbsctx);
+}
+
+void rbbson_free(RBBSON* rbbson) {
+    ruby_xfree(rbbson);
+}
+
 
 VALUE createBsonContextWrap(bson* bsonval, VALUE rbobj, VALUE traverse, int flags) {
     if (NIL_P(bsonContextClass)) {
         rb_raise(rb_eRuntimeError, "Ruby to BSON library must be initialized");
     }
-    VALUE bsonContextWrap = Data_Wrap_Struct(bsonContextClass, NULL, NULL, ruby_xmalloc(sizeof(RBBSON_CONTEXT)));
+    VALUE bsonContextWrap = Data_Wrap_Struct(bsonContextClass, NULL, rbbson_context_free, ruby_xmalloc(sizeof(RBBSON_CONTEXT)));
 
     RBBSON_CONTEXT* rbbsctx;
     Data_Get_Struct(bsonContextWrap, RBBSON_CONTEXT, rbbsctx);
@@ -64,7 +72,7 @@ VALUE createBsonWrap(bson* bsonval) {
     if (NIL_P(bsonWrapClass)) {
         rb_raise(rb_eRuntimeError, "Ruby to BSON library must be initialized");
     }
-    VALUE bsonWrap = Data_Wrap_Struct(bsonWrapClass, NULL, NULL, ruby_xmalloc(sizeof(RBBSON)));
+    VALUE bsonWrap = Data_Wrap_Struct(bsonWrapClass, NULL, rbbson_free, ruby_xmalloc(sizeof(RBBSON)));
     RBBSON* rbbson;
     Data_Get_Struct(bsonWrap, RBBSON, rbbson);
 
index dac6da4..d2ae8f9 100644 (file)
@@ -551,6 +551,9 @@ VALUE EJDB_get_db_meta(VALUE self) {
         }
     }
     rb_hash_aset(res,  rb_str_new2("file"), rb_str_new2(ejdb->metadb->hdb->path));
+
+    tclistdel(cols);
+
     return res;
 }
 
@@ -628,8 +631,10 @@ VALUE EJDB_check_valid_oid_string(VALUE clazz, VALUE oid) {
 
 void close_ejdb_results_internal(RBEJDB_RESULTS* rbres) {
     tclistdel(rbres->results);
+    rbres->results = NULL;
     if (rbres->log) {
         tcxstrdel(rbres->log);
+        rbres->log = NULL;
     }
 }
 
@@ -681,9 +686,6 @@ void EJDB_results_close(VALUE self) {
     Data_Get_Struct(self, RBEJDB_RESULTS, rbresults);
 
     close_ejdb_results_internal(rbresults);
-
-    rbresults->results = NULL;
-    rbresults->log = NULL;
 }