Use SCOPE_EXIT macros to ensure resources are freed

Change-Id: I163799f1a63020199360efcc5084560fe73923dd