Fixes invalid write in case of memory exaustion.