Use boost::scoped_ptr to avoid resource leaks in case of exception

Change-Id: I05a17a061bfd4c3dbd15fe80c1f1f4f8d8267ea7