Issue with 'mg_timer_init()' #2484
-
Hello folks. I use struct mg_mgr mgr;
struct mg_timer fixed_timer;
...
mg_timer_init (&mgr.timers, &fixed_timer, 5000, MG_TIMER_ONCE, net_timeout, 0);
...
static void net_timeout (void *fn_data)
{
// set a flag to cause an 'exit(1)'
} in my program and I build with ASAN (Address Sanitation) on MSVC and clang-cl. The issue I have is with
So how does Mongoose differentiate between user-added timers or timers added by --- a/externals/mongoose.h
+++ b/externals/mongoose.h
@@ -950,6 +950,7 @@ struct mg_timer {
uint64_t period_ms; // Timer period in milliseconds
uint64_t expire; // Expiration timestamp in milliseconds
unsigned flags; // Possible flags values below
+ unsigned added; // Createad by 'mg_timer_add()'
#define MG_TIMER_ONCE 0 // Call function once
#define MG_TIMER_REPEAT 1 // Call function periodically
--- a/externals/mongoose.c
+++ b/externals/mongoose.c
@@ -4291,6 +4291,7 @@ struct mg_timer *mg_timer_add(struct mg_mgr *mgr, uint64_t milliseconds,
if (t != NULL) {
mg_timer_init(&mgr->timers, t, milliseconds, flags, fn, arg);
t->id = mgr->timerid++;
+ t->added = 1;
}
return t;
}
@@ -4298,7 +4299,13 @@ struct mg_timer *mg_timer_add(struct mg_mgr *mgr, uint64_t milliseconds,
void mg_mgr_free(struct mg_mgr *mgr) {
struct mg_connection *c;
struct mg_timer *tmp, *t = mgr->timers;
- while (t != NULL) tmp = t->next, free(t), t = tmp;
+ while (t != NULL) {
+ tmp = t->next;
+ if (t->added)
+ free(t);
+ t = tmp;
+ }
+
mgr->timers = NULL; // Important. Next call to poll won't touch timers
for (c = mgr->conns; c != NULL; c = c->next) c->is_closing = 1; |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
We should perhaps clarify the documentation. |
Beta Was this translation helpful? Give feedback.
-
This looks weird. And how to postpone a timer? I now use code like this: mg_timer *t = &fixed_timer;
t->expire = SOME_TIMEOUT_INTO_THE_FUTURE + mg_millis(); But it's not correct with the current |
Beta Was this translation helpful? Give feedback.
We should perhaps clarify the documentation.
mg_timer_init() is intended to be called to init a timer that was previously added by mg_timer_add(), as it is the latter who allocs memory for the timer.
If you just need a timer "handy", add it with a very long timeout so it does not fire, and then set init it as you need it when you need it