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.

asked Dec 7, 2016 at 0:59
4
  • 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? Commented Dec 7, 2016 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. Commented Dec 7, 2016 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]; Commented Dec 7, 2016 at 9:45
  • Ah yes, you are probably right there. :) Commented Dec 7, 2016 at 22:19

1 Answer 1

0
 actions[i] == 0;

That statement has no useful effect. Perhaps you mean:

actions[i] = 0;
answered Dec 7, 2016 at 6:38
1
  • oh god, man, you saved my life! Sometimes I feel my eyes need glasses ... Such a mistake, it works perfectly now! Commented Dec 7, 2016 at 9:42

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.