Skip to content

Commit d22dfce

Browse files
nobuyui-knk
authored andcommitted
Fix memory leak of rb_ast_t in parser
Do not allocate `rb_ast_t` in `ast_alloc` to avoid memory leak. For example: 10.times do 100_000.times do eval("") end puts `ps -o rss= -p #{$$}` end Before: 17568 20960 24096 27808 31008 34160 37312 40464 43568 46816 After: 14432 14448 14496 14576 14592 15072 15072 15072 15072 15088
1 parent 5108bed commit d22dfce

File tree

1 file changed

+6
-9
lines changed

1 file changed

+6
-9
lines changed

ruby_parser.c

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -784,13 +784,7 @@ static const rb_data_type_t ast_data_type = {
784784
static VALUE
785785
ast_alloc(void)
786786
{
787-
rb_ast_t *ast;
788-
VALUE vast = TypedData_Make_Struct(0, rb_ast_t, &ast_data_type, ast);
789-
#ifdef UNIVERSAL_PARSER
790-
ast = (rb_ast_t *)DATA_PTR(vast);
791-
ast->config = &rb_global_parser_config;
792-
#endif
793-
return vast;
787+
return TypedData_Wrap_Struct(0, &ast_data_type, NULL);
794788
}
795789

796790
VALUE
@@ -1142,8 +1136,11 @@ parser_aset_script_lines_for(VALUE path, rb_parser_ary_t *lines)
11421136
VALUE
11431137
rb_ruby_ast_new(const NODE *const root)
11441138
{
1145-
VALUE vast = ast_alloc();
1146-
rb_ast_t *ast = DATA_PTR(vast);
1139+
rb_ast_t *ast;
1140+
VALUE vast = TypedData_Make_Struct(0, rb_ast_t, &ast_data_type, ast);
1141+
#ifdef UNIVERSAL_PARSER
1142+
ast->config = &rb_global_parser_config;
1143+
#endif
11471144
ast->body = (rb_ast_body_t){
11481145
.root = root,
11491146
.frozen_string_literal = -1,

0 commit comments

Comments
 (0)