Fixed memleak, unified cleanup in case of startup errors.

Change-Id: Ib37d0574e00d2610d5cb2da7ebd59deedf1e8957