defer statement executes only when function return, and the resource still be hold during loop. Release the resource manually when not needed.