0

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.

  • Please post a Minimal, Complete, and Verifiable example. 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:37
  • 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. - I don't understand that at all. – Nick Gammon Dec 7 '16 at 8:53
  • i'd really like to understand, I felt I was declaring a pointer to an array of pointers, not an array of pointers to pointers, I thought an array of pointers to pointers would have been something like Action** actions[10]; – Sierramike Dec 7 '16 at 9:45
  • Ah yes, you are probably right there. :) – Nick Gammon Dec 7 '16 at 22:19
0
  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 Dec 7 '16 at 9:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.