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 7641653
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T08:58:44+00:00 2026-05-31T08:58:44+00:00

Suppose I have a WeakReference of a target strong reference. I’d like to be

  • 0

Suppose I have a WeakReference of a target strong reference. I’d like to be informed when the target object itself is being collected by the GC. Is it possible?

EDIT: Adding code to the finalizer/destructor is not an option here. I need something that is not dependent on class code.

  • 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-31T08:58:45+00:00Added an answer on May 31, 2026 at 8:58 am

    It’s possible under .NET 4.0 and following using ConditionalWeakTable<TKey, TValue>. Thanks this, and other sites. It follows proof of concept code:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Runtime.CompilerServices;
    
    namespace Test
    {
        public static class GCInterceptor
        {
            private static ConditionalWeakTable<object, CallbackRef> _table;
    
            static GCInterceptor()
            {
                _table = new ConditionalWeakTable<object, CallbackRef>();
            }
    
            public static void RegisterGCEvent(this object obj, Action<int> action)
            {
                CallbackRef callbackRef;
                bool found = _table.TryGetValue(obj, out callbackRef);
                if (found)
                {
                    callbackRef.Collected += action;
                    return;
                }
    
                int hashCode = RuntimeHelpers.GetHashCode(obj);
                callbackRef = new CallbackRef(hashCode);
                callbackRef.Collected += action;
                _table.Add(obj, callbackRef);
            }
    
            public static void DeregisterGCEvent(this object obj, Action<int> action)
            {
                CallbackRef callbackRef;
                bool found = _table.TryGetValue(obj, out callbackRef);
                if (!found)
                    throw new Exception("No events registered");
    
                callbackRef.Collected -= action;
            }
    
            private class CallbackRef
            {
                private int _hashCode;
                public event Action<int> Collected;
    
                public CallbackRef(int hashCode)
                {
                    _hashCode = hashCode;
                }
    
                ~CallbackRef()
                {
                    Action<int> handle = Collected;
                    if (handle != null)
                        handle(_hashCode);
                }
            }
        }
    }
    

    Tested with the following code:

    public partial class Form1 : Form
    {
        private object _obj;
    
        public Form1()
        {
            InitializeComponent();
    
            _obj = new object();
    
            _obj.RegisterGCEvent(delegate(int hashCode)
            {
                MessageBox.Show("Object with hash code " + hashCode + " recently collected");
            });
        }
    
        private void button1_Click(object sender, EventArgs e)
        {
            _obj = null;
            GC.Collect();
            GC.WaitForPendingFinalizers();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have a weak reference to a car which has an ordinary (strong)
Suppose I have a class module clsMyClass with an object as a member variable.
I have the following simple code for an object that holds a weak reference:
Suppose have scrolled down to the middle of a web page. Is it possible
Suppose I have a grid of squared defined like so in a class: Square
Suppose I have a data frame, df, that looks like: f t1 t2 t3
Suppose I have a custom object set up in a class similar to this.
Suppose I have a specific table selected in TinyMCE, like this: var ed =
I have a reference to an object. This object has a timer event with
Suppose I have 2 tables, query and rank. I would like to know if

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.