Maybe use scoped ptr for this?