Added missing checking of memory allocation status