Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between a delegate instance and a method pointer?

I thought that a delegate instance was interchangeable with a function instance.

Take the following code:

delegate int AddDelegate(int a, int b);

AddDelegate DelegateInstance;

public void DoStuff()
{
    //I can call this without a delegate "instance":
    MethodThatTakesAdd(Add);

    //I can also call it WITH a delegate "instance"
    DelegateInstance = Add;
    MethodThatTakesAdd(DelegateInstance);
}

public int Add(int a, int b)
{
    return a + b;
}

public void MethodThatTakesAdd(AddDelegate addFunction)
{
    Console.WriteLine(addFunction(1, 2).ToString());
}

Both ways of calling it APPEAR to be equivalent, and if you're using only C#, you'll never see the difference (at least I have not up to this point). However, I was recently unmanaged code that was calling back into this managed code, they are treated differently. For example, in one scenario, I to get the error "A callback was made on a garbage collected delegate" if I use the function directly as a callback (even though my object instance is kept around). Using the "delegate instance" fixes the problem.

Is there someone out there that knows what the difference is?

like image 882
Jason Young Avatar asked Nov 05 '09 16:11

Jason Young


People also ask

What is a delegate instance?

A delegate instance encapsulates an invocation list, which is a list of one or more methods, each of which is referred to as a callable entity. For instance methods, a callable entity consists of an instance and a method on that instance. For static methods, a callable entity consists of just a method.

Is a delegate a function pointer?

A delegate is a type-safe function pointer that can reference a method that has the same signature as that of the delegate. You can take advantage of delegates in C# to implement events and call-back methods. A multicast delegate is one that can point to one or more methods that have identical signatures.

Is delegate a pointer type?

A delegate* type is a pointer type which means it has all of the capabilities and restrictions of a standard pointer type: Only valid in an unsafe context. Methods which contain a delegate* parameter or return type can only be called from an unsafe context. Cannot be converted to object .


2 Answers

Terminology Corretion: Instead of method pointer, the more appropriate term is method group.

In terms of functionality the two statements are equivalent. That is that they produce almost the same IL. The difference is where the delegate value is stored.

In the first case you pass the method group Add to MethodThatTakesAdd directly. This causes a temporary delegate value to be created and then passed to MethodThatTakesAdd. This delegate value is subject to garbage collection the moment the MethodThatTakesAdd returns since it does not store the value.

In the second case you assigned the delegate to a field on the outer instance. This will typically increase the lifetime of the delegate and hence reduce the chance it's garbage collected during your pinvoke call.

like image 129
JaredPar Avatar answered Nov 08 '22 16:11

JaredPar


Delegates are classes that are callable, and have similar behavior to function pointers. The delegate internally stores the address of the function to call (i.e. the function pointer), but also provides other functionality such as multi-casting and storing an invocation list; you can essentially invoke many functions of the same signature with one delegate instance as follows.

public void DoStuff()
{
    DelegateInstance += Add;
    DelegateInstance += AnotherAdd;
    DelegateInstance += YetAnotherAdd;

    // Invoke Add(100, 200), AnotherAdd(100, 200), and YetAnotherAdd(100, 200)
    DelegateInstance(100, 200);
}

Regarding your note about the equivalence of MethodThatTakesAdd(Add) and MethodThatTakesAdd(DelegateInstance), if you look at the MSIL that the C# compiler generates for the line MethodThatTakesAdd(Add), you will notice that the compiler is creating a delegate and wrapping the Add() method for you.

like image 29
Steve Guidi Avatar answered Nov 08 '22 18:11

Steve Guidi