Fix memory lease bugs