Skip to content

[Bug] Memory Leak in Notifications #870

@xoma-zver

Description

@xoma-zver

The behavior shares the same underlying animation retention issue as the Toast problem (#869).

However, WindowNotificationManager has an additional leak caused by the property binding. When creating a NotificationCard, the PositionProperty is bound to the manager. Since WindowNotificationManager is a long-lived object, it maintains active subscriptions to every card created, preventing the Garbage Collector from reclaiming them.

Replacing the indexer binding with an explicit Bind and calling Dispose() when the message closes correctly breaks this connection.

var notificationControl = new NotificationCard
        {
            Content = content,
            NotificationType = type,
            ShowIcon = showIcon,
            ShowClose = showClose,
           // REMOVED: [!NotificationCard.PositionProperty] = this[!PositionProperty]
        };

        // Store the subscription to dispose of it later
        var bindingSubscription = notificationControl.Bind(
            NotificationCard.PositionProperty, 
            this.GetObservable(PositionProperty));
        
        // Add style classes if any
        if (classes is not null)
        {
            foreach (var @class in classes)
            {
                notificationControl.Classes.Add(@class);
            }
        }

        notificationControl.MessageClosed += (sender, _) =>
        {
            onClose?.Invoke();

            // Explicitly unsubscribe to allow GC to collect the control
            bindingSubscription.Dispose();
            
            _items?.Remove(sender);
        };

Metadata

Metadata

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions