I don't quite understand the handover mechanism of `autonomous ()` `disable ()` `opcontrol ()`

I recently taught students to use smart pointers.
The students told me that the smart pointer would often be invalid, if the automatic match was timed out.
if autonomous () and opcontrol () are terminated by field control.
Will the smart pointer be released?
In addition, I found that the “pros:: Task” source code did not write a destructor.
I don’t know about the pros kernel, do I think it’s okay to do that?

~Task()
{
      remove();  //task_delete(task);
}

Destructors are not called when tasks are deleted for both user tasks and autonomous, opcontrol, etc. Memory on the stack will be freed however, and heap memory that was allocated will not.

3 Likes

We don’t define a destructor for the Task object because it would lead to unexpected behavior in cases like this:

/**
 * Convenience function to trigger task
 */
void taskTrigger() {
    pros::Task(myTaskFn);
    // if pros::Task had a destructor defined, the task would end
    // when the object goes out of scope here
}

This is a contrived example, but it should serve to illustrate the motivation.

As @Lachlan240P said, the task cleanup routine does not free any memory you have allocated on the heap. If you are concerned about this, you can look into using task_notify_when_deleting. Note that this function is an advanced feature, and should only be used if you really know what you’re doing.

2 Likes

Oh, I see.

Thank you.
I plan to try decorating “pros:: Task” with destructors.
Then try the smart pointer.His internal implementation is a reference counter.
He’s still built in the heap memory .
And the smart pointer can’t be released manually.
I’m afraid the students will get to the bottom of it. :rofl:
Some students are obsessive-compulsive about code. He wants his code to be “moden c++”. :sweat_smile:
I have to tell them the results of the experiment.

void autonomous()
{
    std::unique_ptr<ncrapi::Task> testTask=std::make_unique<ncrapi::Task>(...);
   pros::delay(15000);
}