Avoid possible memory leaks in case of exceptions

Change-Id: Ie07c6c9989da444eb6052691696e95b808c23fb0