Permalink
Browse files

Fixed memory leak by using Hunspell_free_list on analyze and stem res…

…ults
  • Loading branch information...
1 parent 78a8142 commit 3ac8819e4301a7a27c32435af9b3c5f280536936 @drbrain drbrain committed with Apr 7, 2012
Showing with 14 additions and 13 deletions.
  1. +14 −13 lib/hunspell-ffi.rb
View
@@ -10,6 +10,7 @@ module C
attach_function :Hunspell_add, [:pointer, :string], :int
attach_function :Hunspell_add_with_affix, [:pointer, :string, :string], :int
attach_function :Hunspell_analyze, [:pointer, :pointer, :string], :int
+ attach_function :Hunspell_free_list, [:pointer, :pointer, :int], :void
attach_function :Hunspell_remove, [:pointer, :string], :int
attach_function :Hunspell_stem, [:pointer, :pointer, :string], :int
end
@@ -101,13 +102,19 @@ def analyze(word)
len = C.Hunspell_analyze(@handler, list_pointer, word)
- if len > 0 then
- list = list_pointer.read_pointer
+ read_list(list_pointer, len)
+ end
- list.get_array_of_string 0, len
- else
- []
- end
+ def read_list(list_pointer, len)
+ return [] if len.zero?
+
+ list = list_pointer.read_pointer
+
+ strings = list.get_array_of_string(0, len)
+
+ #C.Hunspell_free_list(@handler, list_pointer, len)
+
+ strings
end
# Remove word from the run-time dictionary
@@ -121,13 +128,7 @@ def stem(word)
len = C.Hunspell_stem(@handler, list_pointer, word)
- if len > 0 then
- list = list_pointer.read_pointer
-
- list.get_array_of_string 0, len
- else
- []
- end
+ read_list(list_pointer, len)
end
end

0 comments on commit 3ac8819

Please sign in to comment.