Fixed memory leaks

closedir() frees the directory entries and it was working before purely by
coincidence.