Permalink
Browse files

fix memory leaks in JSON.generate

Test Script:
{{{
require 'json'

loop do
  JSON.generate("key1" => "foo", "key2" => "bar")
end
}}}
  • Loading branch information...
1 parent c2992da commit 1fa3eba0956090f28939eb29f52e551a6b400f1f @Watson1978 Watson1978 committed Jun 9, 2011
Showing with 25 additions and 1 deletion.
  1. +1 −0 ext/json/generator/extconf.rb
  2. +23 −0 ext/json/generator/generator.c
  3. +1 −1 ext/json/generator/generator.h
@@ -17,4 +17,5 @@
have_header("ruby/re.h")
have_header("ruby/encoding.h")
end
+$INCFLAGS << ' -I../../..'
create_makefile 'json/ext/generator'
@@ -1315,6 +1315,24 @@ static VALUE cState_depth_set(VALUE self, VALUE depth)
return state->depth = FIX2LONG(depth);
}
+#ifdef __MACRUBY__
+#include "macruby_internal.h"
+#include "objc.h"
+static IMP json_generator_state_finalize_imp_super = NULL;
+static void
+json_generator_state_finalize_imp(void *self, SEL sel)
+{
+ JSON_Generator_State *objState;
+ Data_Get_Struct(self, JSON_Generator_State, objState);
+ if (objState != NULL) {
+ State_free(objState);
+ }
+ if (json_generator_state_finalize_imp_super != NULL) {
+ ((void(*)(void *, SEL))json_generator_state_finalize_imp_super)(self, sel);
+ }
+}
+#endif
+
/*
*
*/
@@ -1416,4 +1434,9 @@ void Init_generator()
#endif
i_SAFE_STATE_PROTOTYPE = rb_intern("SAFE_STATE_PROTOTYPE");
CJSON_SAFE_STATE_PROTOTYPE = Qnil;
+
+#ifdef __MACRUBY__
+ json_generator_state_finalize_imp_super = rb_objc_install_method2((Class)cState,
+ "finalize", (IMP)json_generator_state_finalize_imp);
+#endif
}
@@ -7,7 +7,7 @@
#include "ruby.h"
-#if WITH_OBJC
+#ifdef __MACRUBY__
/* We cannot use the GC memory functions here because the underlying libedit
* function will call free() on the memory, resulting in a leak.
*/

0 comments on commit 1fa3eba

Please sign in to comment.