Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -1158,6 +1158,19 @@ ar_clear(VALUE hash)
}
}

static void
st_free_and_clear_table(VALUE hash)
{
HASH_ASSERT(RHASH_ST_TABLE_P(hash));

st_table *tab = RHASH_ST_TABLE(hash);

if (tab->bins != NULL) free(tab->bins);
free(tab->entries);

RHASH_ST_CLEAR(hash);
}

typedef int st_foreach_func(st_data_t, st_data_t, st_data_t);

struct foreach_safe_arg {
Expand Down Expand Up @@ -2894,7 +2907,7 @@ rb_hash_replace(VALUE hash, VALUE hash2)
ar_free_and_clear_table(hash);
}
else {
RHASH_ST_CLEAR(hash);
st_free_and_clear_table(hash);
}

hash_copy(hash, hash2);
Expand Down
2 changes: 1 addition & 1 deletion test/ruby/test_hash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1325,7 +1325,7 @@ def test_replace2
end

def test_replace_memory_leak
assert_no_memory_leak([], "#{<<-"begin;"}", "#{<<-'end;'}")
assert_no_memory_leak([], "#{<<-"begin;"}", "#{<<-'end;'}", rss: true)
h = ("aa".."zz").each_with_index.to_h
10_000.times {h.dup}
begin;
Expand Down