I've read a lot about dynamic allocation for strings or buffers, but that's not my case.
I plan to implement much blinkers types as classes, all inheriting from a base Action
class.
I declare an array of 10 pointers because I want to limit to 10 blinkers simultaneously at most :
Action** actions = new Action*[10];
In the code, I will do something like :
int i = GetFreeActionID();
actions[i] = new ActionLightBlinkerMono(myPin, onDuration, offDuration);
Then the loop will call a method of the object and it works fine.
When it comes to stop the blinking, I would just destroy the object and set the pointer to zero, so the loop will not try to call the method of the object anymore :
if (actions[i]->Do() != 0)
{
delete(actions[i]);
actions[i] == 0;
}
Howerver, I see in the serial console that when it comes to the delete
instruction, the Arduino just reboots itself ...
Tryed also free
instead of delete
with same result.
How could I safely free the object when it's not used anymore ?
It's an evidence I don't know what will be used at compile time, the blinkers will be started by the user (via I2C instructions) at runtime, hence instanciating different classes.
Action** actions = new Action*[10];
- you aren't declaring an array of 10 pointers, you are declaring an array of 10 pointers to pointers. Is this really what you want? – Nick Gammon♦ Dec 7 '16 at 6:37Action** actions[10];
– Sierramike Dec 7 '16 at 9:45