fix deinit sequence to avoid memory leaks

Change-Id: I9a607ae2a709bfb2bb3b49ca6a442bf7f2b11c7f