wfdudley / T-watch-2020

a watch project for the TTGO T-watch-2020 version 1
BSD 3-Clause "New" or "Revised" License
85 stars 27 forks source link

WiFi.onEvent(WiFiEvent); should only be called once #21

Closed FedericoBusero closed 3 years ago

FedericoBusero commented 3 years ago

Each time the app 'NTP Time' or 'Weather' is executed, WiFi.onEvent(WiFiEvent); is executed, but this should only be called once at setup time. If the app NTP time has been opended 10 times, a wifi connection that is created will result in 10x calling the function WiFiEvent, which can be seen on the debug window: you will see 10 times

WiFi connected! IP address: ...

wfdudley commented 3 years ago

Can you explain more about this problem? What is wrong with calling WiFi.onEvent() multiple times? Does it constitute a memory leak?

FedericoBusero commented 3 years ago

The memory allocation will grow each time you call onEvent: it will add a function to the onEvent-callback-list. If you have run the NTP-app 100 times, you will have 100 functions in the onEvent callback list. This means that if you get a connection, the function "WiFiEvent" will be called 100 times!

You can see this in the debug window, you will see the following list growing WiFi connected! IP address: ... WiFi connected! IP address: ... WiFi connected! IP address: ..... WiFi connected! IP address: ..... WiFi connected! IP address: .....

Solution

WiFiEventId_t eventID = WiFi.onEvent(WiFiEvent); ... WiFi.removeEvent(eventID);

wfdudley commented 3 years ago

Thanks for the explanation. I like the "removeEvent()" solution. I'll work on that.

And thanks again for all your help.

Bill Dudley This email is free of malware because I run Linux.

On Mon, Jan 4, 2021 at 11:49 AM FedericoBusero notifications@github.com wrote:

The memory allocation will grow each time you call onEvent: it will add a function to the onEvent-callback-list. If you have run the NTP-app 100 times, you will have 100 functions in the onEvent callback list. This means that if you get a connection, the function "WiFiEvent" will be called 100 times!

You can see this in the debug window, you will see the following list growing WiFi connected! IP address: ... WiFi connected! IP address: ... WiFi connected! IP address: ..... WiFi connected! IP address: ..... WiFi connected! IP address: .....

Solution

  • only call WiFi.onEvent once OR
  • call Wifi.removeEvent when finished

WiFiEventId_t eventID = WiFi.onEvent(WiFiEvent); ... WiFi.removeEvent(eventID);

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/wfdudley/T-watch-2020/issues/21#issuecomment-754086871, or unsubscribe https://github.com/notifications/unsubscribe-auth/AI4VYU6AVDWZFK26UWR4EHDSYHWQZANCNFSM4VS4EYZA .

wfdudley commented 3 years ago

fixed with today's commit. I used WiFi.removeEvent().