SDL joystick detachment

Hello!

I have a question about SDL joystick API.
Let’s imagine the following scenario:

  1. There is one joystick in the system and we open it:
    joy = SDL_JoystickOpen(0);
  2. Some time later we call SDL_JoystickGetAttached(joy) and it returns SDL_FALSE (user has disconnected or reconnected the joystick).
  3. If the joystick is available in the system (user connected it again) we want to reopen it. Should we call SDL_JoystickClose(joy) before opening?

Some demo examples:
Variant 1:
joy = SDL_JoystickOpen(0);
while(1)
{
if (SDL_JoystickGetAttached(joy) == SDL_FALSE)
{
SDL_JoystickClose(joy);
// Other code
// Check if the joystick is available in the system
joy = SDL_JoystickOpen(0);
}
}

Variant 2:
joy = SDL_JoystickOpen(0);
while(1)
{
if (SDL_JoystickGetAttached(joy) == SDL_FALSE)
{
// Other code
// Check if the joystick is available in the system
joy = SDL_JoystickOpen(0);
}
}

Which code is correct?