Improved memory management for hooks.