Revert "Revert "Prevent cursor from causing global reference leaks.""

This reverts commit 3ececd6624ec0df5ebff14103963964276792f86.