Check for nil before calling close