Fixed memory leaks in __sleep and __wakeup