Fix some memory leaks.
Implement kfindres.