Merge "Fix some memory leaks, potential errors." am: 9d239ec6a5 am: 60d677147e
am: 0da25d6feb

Change-Id: I63185b261a0faff1c5408745679cfe68978d5337