Skip to content

Commit bc747dc

Browse files
author
jarlamsa
committed
Check for duplicate status callbacks before adding to the list
Duplicate status callbacks don't bring any value and can cause memory leaks if caller calls for this function multiple times with same status_cb. Also remove all the instances of the status_cb when removing event listener.
1 parent 2660621 commit bc747dc

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

connectivity/netsocket/source/NetworkInterface.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,13 @@ static void call_all_event_listeners(NetworkInterface *iface, nsapi_event_t even
145145
void NetworkInterface::add_event_listener(mbed::Callback<void(nsapi_event_t, intptr_t)> status_cb)
146146
{
147147
iface_eventlist_t *event_list = get_interface_event_list_head();
148+
#if MBED_CONF_PLATFORM_CALLBACK_COMPARABLE
149+
ns_list_foreach_safe(iface_eventlist_entry_t, entry, event_list) {
150+
if (entry->status_cb == status_cb && entry->iface == this) {
151+
return;
152+
}
153+
}
154+
#endif
148155
iface_eventlist_entry_t *entry = new iface_eventlist_entry_t;
149156
entry->iface = this;
150157
entry->status_cb = status_cb;
@@ -160,7 +167,6 @@ void NetworkInterface::remove_event_listener(mbed::Callback<void(nsapi_event_t,
160167
if (entry->status_cb == status_cb && entry->iface == this) {
161168
ns_list_remove(event_list, entry);
162169
delete entry;
163-
return;
164170
}
165171
}
166172
}

0 commit comments

Comments
 (0)