Task memory leak

rebase of pull request #558