fix memory leaks and remove a dead store
Fix memory leaks in creating gradient thanks to @kilink. closes #6