Skip to content

Commit 9060264

Browse files
committed
Use reference-counted pointer in notification callback
`this` could be deleted after `Notification::BeginExecuteNotification` exited and before `Notification::ExecuteNotificationHelper` finished. This is fixed by constructing a `Notification::Ptr` and operate on that one as it is properly reference-counted.
1 parent c6cf9c6 commit 9060264

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

lib/icinga/notification.cpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,11 @@ void Notification::BeginExecuteNotification(NotificationType type, const CheckRe
398398
<< "Sending " << (reminder ? "reminder " : "") << "'" << NotificationTypeToString(type) << "' notification '"
399399
<< notificationName << "' for user '" << userName << "'";
400400

401-
Utility::QueueAsyncCallback(std::bind(&Notification::ExecuteNotificationHelper, this, type, user, cr, force, author, text));
401+
// Explicitly use Notification::Ptr to keep the reference counted while the callback is active
402+
Notification::Ptr notification (this);
403+
Utility::QueueAsyncCallback([notification, type, user, cr, force, author, text]() {
404+
notification->ExecuteNotificationHelper(type, user, cr, force, author, text);
405+
});
402406

403407
/* collect all notified users */
404408
allNotifiedUsers.insert(user);

0 commit comments

Comments
 (0)