added check for heap memory overflow