Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
10 years, 5 months ago.
RtosTimer creation inside subroutine/method not working
Hi, If I run the "official" RtosTimer example everything goes well...but if I start same timers from a subroutine timer will never trigger ! Here's my example tweaking:
#include "mbed.h" #include "rtos.h" DigitalOut LEDs[4] = { DigitalOut(LED1), DigitalOut(LED2), DigitalOut(LED3), DigitalOut(LED4) }; void blink(void const *n) { LEDs[(int)n] = !LEDs[(int)n]; } void startTimers (void) { RtosTimer led_1_timer(blink, osTimerPeriodic, (void *)0); RtosTimer led_2_timer(blink, osTimerPeriodic, (void *)1); RtosTimer led_3_timer(blink, osTimerPeriodic, (void *)2); RtosTimer led_4_timer(blink, osTimerPeriodic, (void *)3); led_1_timer.start(2000); led_2_timer.start(1000); led_3_timer.start(500); led_4_timer.start(250); // Here led_x_timer get destroyed, but I don't think this is a problem, timers are already scheduled and created... } int main(void) { startTimers (); Thread::wait(osWaitForever); }
Can you help me ?
1 Answer
10 years, 5 months ago.
Hello Paolo Bisiach,
your timers as commented, gets destroyed. Because they are created within the startTimers scope (its stack frame), the destructors are invoked, which destroyes all local objects created within stack frame.
Create the objects on heap or globally, your preference..
Regards,
0xc0170