Prevent memory leaks when adding listeners

Check if the callback has already been added to the set before protecting it. Vice versa for unprotecting it when removing the listener.
This commit is contained in:
Scott Kyle 2015-11-03 02:43:29 -08:00
parent 7df2997ee7
commit 83869e2193
1 changed files with 8 additions and 4 deletions

View File

@ -49,13 +49,17 @@ public:
}
void add_notification(JSObjectRef notification) {
if (!m_notifications.count(notification)) {
JSValueProtect(m_context, notification);
m_notifications.insert(notification);
}
}
void remove_notification(JSObjectRef notification) {
if (m_notifications.count(notification)) {
JSValueUnprotect(m_context, notification);
m_notifications.erase(notification);
}
}
void remove_all_notifications() {
for (auto notification : m_notifications) {
JSValueUnprotect(m_context, notification);