WojciechMula/aspell-python

Memory leak

ndvbd opened this issue · 2 comments

ndvbd commented

Memory leak is created when running this code:

import aspell  
import gc
spellChecker = aspell.Speller('lang', 'en')
word = "TNKLR"
for j in range(0,10000):
	collected = gc.collect()
	print('gc')
	for i in range(100000):
		result = spellChecker.suggest(word)
ndvbd commented

I solved the memory leak in the python wrapper. In the file aspell.2.c (or in aspell.c) the AspellWordList2PythonList while loop should be changed to:


	while ( (word=aspell_string_enumeration_next(elements)) != 0) {
	  
	    PyObject *py_word = Py_BuildValue("s", word);

        int result = PyList_Append(list, py_word);
        Py_DECREF(py_word);

		if (result == -1) {
			PyErr_SetString(PyExc_Exception, "It is almost impossible, but happend! Can't append element to the list.");
			delete_aspell_string_enumeration(elements);
			Py_DECREF(list);
			return NULL;
		}
    }

Note that there is a memory leak also in the linux aspell itself, and it should be built using:
GNUAspell/aspell#601
before patching this python wrapper