Skip to content

Commit

Permalink
avoid memory leak with notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
Cvolton committed Aug 15, 2024
1 parent a4a055e commit 0e8cc9c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
9 changes: 9 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,15 @@ class BI_DLL $modify(MenuLayer) {
fixLevelLists();
}

Notification::create("important notification")->show();
BetterInfo::showUnimportantNotification("unimportant 1", NotificationIcon::Success);
BetterInfo::showUnimportantNotification("unimportant 2", NotificationIcon::Success);
BetterInfo::showUnimportantNotification("unimportant 3", NotificationIcon::Success);
BetterInfo::showUnimportantNotification("unimportant 4", NotificationIcon::Success);
BetterInfo::showUnimportantNotification("unimportant 5", NotificationIcon::Success);
BetterInfo::showUnimportantNotification("unimportant 6", NotificationIcon::Success);
Notification::create("important notification 2")->show();

return true;
}
};
Expand Down
16 changes: 12 additions & 4 deletions src/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -887,18 +887,26 @@ long long BetterInfo::strtol(std::string_view str) {
return result;
}

static std::vector<Ref<Notification>> notifications;
static std::vector<Ref<Notification>> s_notifications;
void BetterInfo::showUnimportantNotification(const std::string& content, NotificationIcon icon, float time) {
if(GJBaseGameLayer::get()) return;

auto notif = Notification::create(content, icon, time);
notifications.push_back(notif);
s_notifications.push_back(notif);
notif->show();

std::thread([notif, time] {
//assume up to 2 notifications are scheduled outside of this - if this fails nothing much really happens, they just wont be removed immediately
std::this_thread::sleep_for(std::chrono::seconds((int) (time + (s_notifications.size() + 2))));
Loader::get()->queueInMainThread([notif] {
s_notifications.erase(std::remove(s_notifications.begin(), s_notifications.end(), notif), s_notifications.end());
});
}).detach();
}

void BetterInfo::cancelUnimportantNotifications() {
for(auto& notification : notifications) {
for(auto& notification : s_notifications) {
notification->cancel();
}
notifications.clear();
s_notifications.clear();
}

0 comments on commit 0e8cc9c

Please sign in to comment.