Only clear fields when object allocation succeeds

This makes sure we get a no memory exception when we exhaust memory.

Fixes #2498