Avoid possible memory leaks in case of exceptions

Change-Id: I986805143615f053b918fb1e64b0b24d6f76f2de