Add some defensive programming against memory allocation failures.