Skip to content

Commit

Permalink
fix memory leaks in JSON.generate
Browse files Browse the repository at this point in the history
Test Script:
{{{
require 'json'

loop do
  JSON.generate("key1" => "foo", "key2" => "bar")
end
}}}
  • Loading branch information
Watson1978 committed Jun 9, 2011
1 parent c2992da commit 1fa3eba
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
1 change: 1 addition & 0 deletions ext/json/generator/extconf.rb
Expand Up @@ -17,4 +17,5 @@
have_header("ruby/re.h")
have_header("ruby/encoding.h")
end
$INCFLAGS << ' -I../../..'
create_makefile 'json/ext/generator'
23 changes: 23 additions & 0 deletions ext/json/generator/generator.c
Expand Up @@ -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

/*
*
*/
Expand Down Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion ext/json/generator/generator.h
Expand Up @@ -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.
*/
Expand Down

2 comments on commit 1fa3eba

@lrz
Copy link
Member

@lrz lrz commented on 1fa3eba Jun 10, 2011

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should not be needed if http://www.macruby.org/trac/ticket/1308 is fixed, right?

@Watson1978
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, you are right :)

Please sign in to comment.