Fixes memory leaks pointed out by the static analyser