Resource leak with joystick buttons

My robot OI uses an XboxController, and to access the buttons, I use code like this:

As you can see, the code is underlined with a “Resource leak” warning. I know this means I’m supposed to close the resource, but since the buttons are used throughout the lifespan of the program, I’m not sure where to close the buttons in my code. I’m fairly confident this is not a vscode error; I’ve cleaned the java workspace and recompiled multiple times. For some reason, this doesn’t show up in some other code which uses a JoystickController. Can someone help me out?

Its just flagging them because they implement Java’s AutoClosable interface. You can safely ignore it for those cases, and should. There is nowhere in the wpilib templates to close your resources, as the robot loop never stops.

1 Like

Wow that was fast, thank you very much! I guess I should be more concerned about the recent blue screen I had then…

Actually, I answered to quickly. I didn’t realize we had made RobotBase implement AutoClosable as well. So you can actually just override the close method in your robot class, and call close() on those objects in there.

Oh ok cool, that makes sense. Thanks again!