Fix resource leaks

Change-Id: I3a201cfd924e467d605351782bd06874c2af725e