Use __attribute__(cleanup) to simplify memory cleanup