Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix memory leak in error::raise #30

Closed
wants to merge 2 commits into from

Conversation

ianks
Copy link
Contributor

@ianks ianks commented Nov 14, 2022

When running ruby_memcheck against wasmtime-rb's test suite, and Valgrind detected a bunch of small leaks coming from `error::raise.

Turns out, rb_raise never frees the error message buffer so the memory for every raised Error::Error leaks.

To fix this, error:raise now uses rb_exc_new_str which will allow the Ruby GC to free the RString error message. With this change, Valgrind no longer detects the leak.

When running [`ruby_memcheck`] against [`wasmtime-rb`]'s test suite, and
Valgrind [detected a bunch of small leaks coming from
`error::raise](https://github.com/bytecodealliance/wasmtime-rb/actions/runs/3457450066/jobs/5770886620).

Turns out, `rb_raise` never frees the error message buffer so the memory for
every raised `Error::Error` leaks.

To fix this, `error:raise` now uses `rb_exc_new_str` which will allow the Ruby
GC to free the `RString` error message. With this change, [Valgrind no longer
detects the leak](https://github.com/bytecodealliance/wasmtime-rb/actions/runs/3457832824/jobs/5771647954).

[`ruby_memcheck`]: https://github.com/Shopify/ruby_memcheck
[`wasmtime-rb`]: https://github.com/bytecodealliance/wasmtime-rb
@matsadler
Copy link
Owner

Oh wow, I went to such great lengths to avoid leaks when raising errors (i.e. the whole design of returning errors to raise), and then right there at the very last hurdle there's a leak just staring me in the face.

I took a slightly different tack and (assuming no further oversights) fixed this in 8c870e3.

@matsadler matsadler closed this Nov 14, 2022
@ianks
Copy link
Contributor Author

ianks commented Nov 14, 2022

Awesome, thanks @matsadler. Seems to resolve the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants