Can the Old CommandGroup Framework Cause Memory Leaks?

As only our second year as a Robot team (FRC Team #7464 - ORION), this is the first year we needed to make a command group.

We are still using the old command-based programming, and plan to migrate to the new command-based programming in the off-season.

However, I was reviewing a command group, and had a question about memory leaks.

Let’s say I had some commands called
GoStraight(double speed, double distance) and
Turn(double speed, double angle)

Let’s also say I have a command group called DefaultCmdGrp(). Per this years documentation (and last years as well), let’s say that we have:

DefaultCmdGrp::DefaultCmdGrp() {

  AddSequential(new GoStraight(0.1, 5));
  AddSequential(new Turn(0.2, 45));
  AddSequential(new GoStraight(0.3, 10));

}

DefaultCmdGrp::~DefaultCmdGrp() {
}

When the command group DefaultCmdGrp() gets deleted, won’t the memory for the 3 command instances be still committed, and not returned to the memory pool? I didn’t see any delete calls in the destructors of CommandGroup, Command, or ErrorBase.

Would it not be better to do something like (please forgive the pseudo code):

DefaultCmdGrp::DefaultCmdGrp() {

  command1 = new GoStraight(0.1, 5);
  command2 = new Turn(0.2, 45);
  command3 = new GoStraight(0.3, 10);

  AddSequential(command1);
  AddSequential(command2);
  AddSequential(command3);

}

DefaultCmdGrp::~DefaultCmdGrp() {

  delete command3;
  delete command2;
  delete command1;

}

private:
  Command *command1;
  Command *command2;
  Command *command3;

It’s been a while (14 years) since I’ve done some serious C++ coding. Am I missing something? Or just forgot about memory management in C++?

A confused mentor…

Yes, the old framework was very leaky. Generally it doesn’t matter because robot code never exits, and commands are only created at the beginning of the robot program. It is problematic for unit testing, so the new framework is much better about this.

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.