Correctly release resources to prevent memory leak and prevent double destroying