Skip to content

Commit cf19102

Browse files
authored
[commands] SelectCommand: Fix leakage and multiple composition bug (wpilibsuite#5571)
1 parent 171375f commit cf19102

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/SelectCommand.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ public class SelectCommand extends Command {
2727
private boolean m_runsWhenDisabled = true;
2828
private InterruptionBehavior m_interruptBehavior = InterruptionBehavior.kCancelIncoming;
2929

30+
private final Command m_defaultCommand =
31+
new PrintCommand("SelectCommand selector value does not correspond to any command!");
32+
3033
/**
3134
* Creates a new SelectCommand.
3235
*
@@ -37,6 +40,7 @@ public SelectCommand(Map<Object, Command> commands, Supplier<Object> selector) {
3740
m_commands = requireNonNullParam(commands, "commands", "SelectCommand");
3841
m_selector = requireNonNullParam(selector, "selector", "SelectCommand");
3942

43+
CommandScheduler.getInstance().registerComposedCommands(m_defaultCommand);
4044
CommandScheduler.getInstance()
4145
.registerComposedCommands(commands.values().toArray(new Command[] {}));
4246

@@ -51,12 +55,7 @@ public SelectCommand(Map<Object, Command> commands, Supplier<Object> selector) {
5155

5256
@Override
5357
public void initialize() {
54-
if (!m_commands.containsKey(m_selector.get())) {
55-
m_selectedCommand =
56-
new PrintCommand("SelectCommand selector value does not correspond to any command!");
57-
} else {
58-
m_selectedCommand = m_commands.get(m_selector.get());
59-
}
58+
m_selectedCommand = m_commands.getOrDefault(m_selector.get(), m_defaultCommand);
6059
m_selectedCommand.initialize();
6160
}
6261

wpilibNewCommands/src/main/native/include/frc2/command/SelectCommand.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,10 @@ class SelectCommand : public CommandHelper<Command, SelectCommand<Key>> {
5454
std::make_unique<std::decay_t<Commands>>(std::move(commands.second))),
5555
...);
5656

57+
m_defaultCommand.SetComposed(true);
5758
for (auto&& command : foo) {
5859
CommandScheduler::GetInstance().RequireUngrouped(command.second.get());
60+
command.second.get()->SetComposed(true);
5961
}
6062

6163
for (auto&& command : foo) {
@@ -73,8 +75,10 @@ class SelectCommand : public CommandHelper<Command, SelectCommand<Key>> {
7375
std::function<Key()> selector,
7476
std::vector<std::pair<Key, std::unique_ptr<Command>>>&& commands)
7577
: m_selector{std::move(selector)} {
78+
m_defaultCommand.SetComposed(true);
7679
for (auto&& command : commands) {
7780
CommandScheduler::GetInstance().RequireUngrouped(command.second.get());
81+
command.second.get()->SetComposed(true);
7882
}
7983

8084
for (auto&& command : commands) {
@@ -139,14 +143,16 @@ class SelectCommand : public CommandHelper<Command, SelectCommand<Key>> {
139143
bool m_runsWhenDisabled = true;
140144
Command::InterruptionBehavior m_interruptBehavior{
141145
Command::InterruptionBehavior::kCancelIncoming};
146+
147+
PrintCommand m_defaultCommand{
148+
"SelectCommand selector value does not correspond to any command!"};
142149
};
143150

144151
template <typename T>
145152
void SelectCommand<T>::Initialize() {
146153
auto find = m_commands.find(m_selector());
147154
if (find == m_commands.end()) {
148-
m_selectedCommand = new PrintCommand(
149-
"SelectCommand selector value does not correspond to any command!");
155+
m_selectedCommand = &m_defaultCommand;
150156
} else {
151157
m_selectedCommand = find->second.get();
152158
}

0 commit comments

Comments
 (0)