Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NullReferenceException during C++ callback to C# function

developers!
I have very strange problem. My project has DLL writen in C++ and a GUI writen in C#. And I have implemented callback for some interoperability. I planed that C++ dll will call C# code in some circumstances. It works... but not long and I cant understand why. The problem marked in comment in C# part
Here the complete code of simplified sample:

C++ DLL:

#include <SDKDDKVer.h>
#define WIN32_LEAN_AND_MEAN
#include <windows.h>

BOOL APIENTRY DllMain( HMODULE hModule,
                    DWORD  ul_reason_for_call,
                    LPVOID lpReserved
                                    )
    {
    switch (ul_reason_for_call)
    {
        case DLL_PROCESS_ATTACH:
        case DLL_THREAD_ATTACH:
        case DLL_THREAD_DETACH:
        case DLL_PROCESS_DETACH:
            break;
    }
    return TRUE;
}

extern "C" 
{    
    typedef void  (*WriteSymbolCallback) (char Symbol); 
    WriteSymbolCallback Test;

    _declspec(dllexport) void InitializeLib()
    {
        Test = NULL;
    }

    _declspec(dllexport) void SetDelegate(WriteSymbolCallback Callback)
    {
        Test = Callback;
    }

    _declspec(dllexport) void TestCall(const char* Text,int Length)
    {
        if(Test != NULL)
        {
            for(int i=0;i<Length;i++)
            {
                Test(Text[i]);
            }
        }
    }
};

C# part:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace CallBackClient
{
    class Program
    {
        [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
        private delegate void WriteToConsoleCallback(char Symbol);

        [DllImport("CallbackSketch.dll",CharSet=CharSet.Ansi,CallingConvention=CallingConvention.Cdecl)]
        private static extern void InitializeLib();

        [DllImport("CallbackSketch.dll",CharSet=CharSet.Ansi,CallingConvention=CallingConvention.Cdecl)]
        private static extern void SetDelegate(WriteToConsoleCallback Callback);

        [DllImport("CallbackSketch.dll",CharSet=CharSet.Ansi,CallingConvention=CallingConvention.Cdecl)]
        private static extern void TestCall(string Text,int Length);

        private static void PrintSymbol(char Symbol)
        {
            Console.Write(Symbol.ToString());
        }

        static void Main(string[] args)
        {
            InitializeLib();
            SetDelegate(new WriteToConsoleCallback(PrintSymbol));

            string test = "Hello world!";


            for (int i = 0; i < 15000; i++)
            {
                TestCall(test, test.Length);// It crashes when i == 6860!!!! Debugger told me about System.NullReferenceException
            }            
        }
    }
}

The problem is that it crashes in 6860th iteration! I believe that the problem is lack of my knowlege in the subject. Could sombody help me?

like image 636
Anton Semenov Avatar asked Feb 05 '11 12:02

Anton Semenov


1 Answers

       SetDelegate(new WriteToConsoleCallback(PrintSymbol));

Yes, this cannot work properly. The native code is storing a function pointer for the delegate object but the garbage collector cannot see this reference. As far as it is concerned, there are no references to the object. And the next collection destroys it. Kaboom.

You have to store a reference to the object yourself. Add a field in the class to store it:

    private static WriteToConsoleCallback callback;

    static void Main(string[] args)
    {
        InitializeLib();
        callback = new WriteToConsoleCallback(PrintSymbol);
        SetDelegate(callback);
        // etc...
    }

The rule is that the class that stores the object must have a lifetime at least as long as native code's opportunity to make the callback. It must be static in this particular case, that's solid.

like image 65
Hans Passant Avatar answered Oct 06 '22 01:10

Hans Passant