fix memory leak and undefined behavior by accesing compound literal outside block scope