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.
1 Answer 1
actions[i] == 0;
That statement has no useful effect. Perhaps you mean:
actions[i] = 0;
-
oh god, man, you saved my life! Sometimes I feel my eyes need glasses ... Such a mistake, it works perfectly now!Sierramike– Sierramike2016年12月07日 09:42:59 +00:00Commented Dec 7, 2016 at 9:42
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?Action** actions[10];