Avoid possible memory leaks in case of exceptions

Change-Id: Id0304366c4e6191db85527935f5bc5cdb0aeb8d8