Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Reference Counting

I am looking for a good pattern to implement ref counting in C#. I have a

Dictionary<string, MyObject> ObjList;

What I want to do is hand out references to MyObject instances if they exist and create new ones if they don't. Multiple places in my code may make a reference to a MyObject instance but when all are freed up I would like to remove it from my dictionary. I have looked into WeakReference but Im not sure if it is applicable here??

Edit 1.) Specifics I am using an OPC Server so whenever I reference an Item I would like to use the dictionary to lookup existing references to the item. When I dont need the item anymore I want to unsubscribe from the item. It isnt easy to know when or how many place in my code are currently using the item.

like image 251
MDK Avatar asked Jan 26 '16 00:01

MDK


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C full form?

Full form of C is “COMPILE”. One thing which was missing in C language was further added to C++ that is 'the concept of CLASSES'.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What is C language basics?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.


2 Answers

What I have done in these scenarios is create a dictionary of WeakReferences. What a WeakReference does is allow your dictionary to refer to an instance of MyObject without keeping that instance in memory. Therefore, once all the other references to that object have been freed, an entry for the item will still exist in the dictionary. However, that entry will refer to a WeakReference where IsAlive is false and the Target is null. If you want, you can occasionally clean up the dictionary by removing all the entries where the WeakReference is not alive.

like image 136
erdomke Avatar answered Oct 30 '22 16:10

erdomke


This is the kind of thing you need:

public class WeakReferences<T>
{
    private Func<string, T> _factory;

    public WeakReferences(Func<string, T> factory)
    {
        _factory = factory;
    }

    private Dictionary<string, WeakReference> _references =
        new Dictionary<string, WeakReference>();

    public T this[string index]
    {
        get
        {
            T target = default(T);
            if (_references.ContainsKey(index))
            {
                var wr = _references[index];
                target = (T)wr.Target;
                if (wr.IsAlive)
                {
                    Console.WriteLine("Reused: " + index);
                    return target;
                }
            }
            target = _factory(index);
            _references[index] = new WeakReference(target);
            return target;
        }
    }
}

You can use it like this:

    Func<string, object> f = k =>
    {
        Console.WriteLine("Created: " + k);
        return new object();
    };

    var wrs = new WeakReferences<object>(f);

    var a = wrs["a"];
    var b = wrs["b"];
    a = wrs["a"];
    b = wrs["b"];
    a = null;
    GC.Collect();
    a = wrs["a"];
    b = wrs["b"];

The output I get running this is:

Created: a
Created: b
Reused: a
Reused: b
Created: a
Reused: b
like image 28
Enigmativity Avatar answered Oct 30 '22 16:10

Enigmativity