Fixed potential NULL pointer dereference if memory allocation fails