Naive Bayes memory leak fix

This is a quite important memory leak fix, found with valgrind. It would be nice to merge to both 2.4 and master.