fixed: some issues pointed out by valgrind

SPLIT ME
Merge pull request #5051 from FernetMenta/valgrind

fixed: some issues pointed out by valgrind