esp_event: fix crash when unregistering a handler instance in itself
esp_event: fix crash when unregistering a handler instance in itself

When a handler instance is the last one in the list und unregisters
itself, the handler iterator will be invalidated by entering free'd
memory. Same applies for event base and id, if they become empty.

Merges https://github.com/espressif/esp-idf/pull/4139

espressif/esp-idf,espressif/esp-idf,espressif/esp-idf,espressif/esp-idf