Avoid possible memory leaks in case of exceptions

Change-Id: Ifd98e11380bf00f6126e4c81bcd0cbaf5b22f70d