Last active
January 29, 2018 16:29
-
-
Save skatkov/662fd1cfd76543da3afe0cf22a3b7a6e to your computer and use it in GitHub Desktop.
String removal from malloc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class String | |
def auto_clear | |
begin | |
result = yield self | |
result.equal?(self) ? nil : result # avoid leaking | |
ensure | |
clear | |
end | |
end | |
end | |
#Then people can do this with Strings whose lifetime they know. | |
expr_returning_string.auto_clear do |str| | |
puts "We got #{str}." | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment