Fix resource leak issue for cursors.

Fix resource leak issue for cursors.