Add debug logging.
Fix use-after-free.