Check the return values where memory allocation failures may happen.