Closed benjiboy50fonz closed 3 years ago
If I had to guess, I'd say SmartDashboard.putData is not keeping objects passed in alive. Since the Python code is not keeping a reference to the object, it gets freed, and once SmartDashboard.updateValues is called (by TimedRobot), you end up with an attempted use-after-free.
I can't seem to get this to crash on Linux, might be because Windows is a bit stricter on how memory is used.
The keepalive is commented out https://github.com/robotpy/robotpy-wpilib/blob/main/gen/SmartDashboard.yml#L27.
If a reference to cmd
isn't kept, it does crash for me on Windows.
@benjiboy50fonz this isn't valid code. If you putData with a command that is immediately deleted... it's immediately deleted. What are you expecting showCommand
to actually do here? Nothing is keeping the command alive, so even if it didn't crash it shouldn't show anything on NetworkTables.
@virtuald Oh I get it now. Thank you guys, and my apologies everyone.
Lol. Weird use case but
from commands2 import TimedCommandRobot, InstantCommand
from wpilib._impl.main import run
from wpilib import SmartDashboard
class Robot(TimedCommandRobot):
def robotInit(self):
self.myCommand = InstantCommand(lambda: print("hello"))
# SmartDashboard.putData("mycommand", self.myCommand) # WORKS
SmartDashboard.putData("mycommand", InstantCommand(lambda: print("hello"))) # DOESNT WORK
def teleopInit(self) -> None:
SmartDashboard.getData("mycommand").schedule()
if __name__ == "__main__":
run(Robot)
I guess teams may run into this if they put a bunch of autonomous commands into networktables and use shuffleboard as an auto selector. But, idk if anyone should/will do this.
use shuffleboard as an auto selector
Doesn't SendableChooser keep references to its objects?
Yes, SendableChooser contains py::object
, which will ensure it doesn't remove references.
I think the reason I hadn't used keepalive for putData was because of the potential to just keep putting the same thing over and over again and leaking like crazy. I think the appropriate way to deal with this is to have a hidden dictionary that putData stores the object in. That way, if you keep creating new objects and putting them on the same key they'll stay alive and the old objects will be deleted.
Still won't likely lead to anything useful, but will at least enable a putData/getData combination to work as expected.
Hi devs, @virtuald ,
Managed to isolate this issue. It's rather odd; about two-thirds of the time it provides me with an access violation, and the other third provides me with this pointer issue. Nevertheless, the issue seems to be stemming from the creation of an object within what I assume is a static function, the putData() in SmartDashboard.
My OS is Windows 10, and my packages are up to date. My
wpilib
package is version 2021.2.2.0 and myrobotpy-commands-v2
package is version 2021.2.2.0.The error looks like so:
The code to replicate this issue is this:
Again, this code appears to be rather unstable and unpredictable.
Thanks!