Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6548647
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:59:44+00:00 2026-05-25T11:59:44+00:00

What I want to do is ensure that if the only reference to my

  • 0

What I want to do is ensure that if the only reference to my observer is the observable, it get’s garbage collected and stops receiving messages.

Say I have a control with a list box on it called Messages and this code behind:

//Short lived display of messages (only while the user's viewing incoming messages)
public partial class MessageDisplay : UserControl
{
    public MessageDisplay()
    {
        InitializeComponent();
        MySource.IncomingMessages.Subscribe(m => Messages.Items.Add(m));
    }
}

Which is connecting to this source:

//Long lived location for message store
static class MySource
{
    public readonly static IObservable<string> IncomingMessages = new ReplaySubject<string>;
}

What I don’t want is to have the Message Display being kept in memory long after it’s no longer visible. Ideally I’d like a little extension so I can write:

MySource.IncomingMessages.ToWeakObservable().Subscribe(m => Messages.Items.Add(m));

I also don’t want to rely on the fact that MessageDisplay is a user control as I will later want to go for an MVVM setup with MessageDisplayViewModel which won’t be a user control.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T11:59:44+00:00Added an answer on May 25, 2026 at 11:59 am

    The code below is inspired by dtb’s original post. The only change is that it returns a reference to the observer as part of the IDisposable. This means that the reference to the IObserver will be kept alive as long as you keep a reference to the IDisposable that you get out at the end of the chain (assuming all disposables keep a reference to the disposable before them). This allows the usage of the extension methods such as Subscribe(M=>DoSomethingWithM(M)) because we keep a reference to the implicitly constructed IObserver but we don’t keep a strong reference from the source to the IObserver (which would produce a memory leek).

    using System.Reactive.Linq;
    
    static class WeakObservation
    {
        public static IObservable<T> ToWeakObservable<T>(this IObservable<T> observable)
        {
            return Observable.Create<T>(observer =>
                (IDisposable)new DisposableReference(new WeakObserver<T>(observable, observer), observer)
                );
        }
    }
    
    class DisposableReference : IDisposable
    {
        public DisposableReference(IDisposable InnerDisposable, object Reference)
        {
            this.InnerDisposable = InnerDisposable;
            this.Reference = Reference;
        }
    
        private IDisposable InnerDisposable;
        private object Reference;
    
        public void Dispose()
        {
            InnerDisposable.Dispose();
            Reference = null;
        }
    }
    
    class WeakObserver<T> : IObserver<T>, IDisposable
    {
        private readonly WeakReference reference;
        private readonly IDisposable subscription;
        private bool disposed;
    
        public WeakObserver(IObservable<T> observable, IObserver<T> observer)
        {
            this.reference = new WeakReference(observer);
            this.subscription = observable.Subscribe(this);
        }
    
        public void OnCompleted()
        {
            var observer = (IObserver<T>)this.reference.Target;
            if (observer != null) observer.OnCompleted();
            else this.Dispose();
        }
    
        public void OnError(Exception error)
        {
            var observer = (IObserver<T>)this.reference.Target;
            if (observer != null) observer.OnError(error);
            else this.Dispose();
        }
    
        public void OnNext(T value)
        {
            var observer = (IObserver<T>)this.reference.Target;
            if (observer != null) observer.OnNext(value);
            else this.Dispose();
        }
    
        public void Dispose()
        {
            if (!this.disposed)
            {
                this.disposed = true;
                this.subscription.Dispose();
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have run into a situation where I want to ensure that a compound
I have a windows installer (MSI) project. I want to ensure that when i
I have a C# Windows application that I want to ensure will show up
I have a JSF 2.0 page that I want to allow a GET parameter
I want to implement a JMS Queue to ensure that each message is only
I have a timer in my class that is I only ever want one
We had a bit of a problem. :) We want to ensure that only
I want to ensure that a division of integers is always rounded up if
I want to ensure that a WCF-ServiceClient State will be closed after using the
I want to ensure that there are no duplicate book titles in the following

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.