WojciechMula / aspell-python

Python wrapper for aspell (C extension and python version)
BSD 3-Clause "New" or "Revised" License
81 stars 20 forks source link

Memory leak #23

Open ndvbd opened 1 year ago

ndvbd commented 1 year ago

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 1 year ago

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: https://github.com/GNUAspell/aspell/pull/601 before patching this python wrapper

ndvbd commented 1 year ago

https://github.com/WojciechMula/aspell-python/pull/24