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

Change-Id: I184799dd198d6469326f0c07dfb15a5323e9af69