title: CallBack on garbagecollected Delegate
body: <p>I'm working with a usb device. This Device receives messages and I don't know when or how often. The API that comes with the driver specifies a setreceiveCallBack function that gives a callback when the device receives a message.
  But at random times or intervals I receive a callback on garbagecollected delegate exeption. I have searched for solutions to my problem but none of the solutions seem to work in my case.
  The following is the biggest part of my code:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Data;
  using System.Drawing;
  using System.IO;
  using System.Linq;
  using System.Text;
  using System.Runtime.InteropServices;
  using System.Windows.Forms;
  
  namespace CallBacktesting
  {
      public unsafe delegate void callBack(Form1.CANMsg *pmsg);
  
      public partial class Form1 : Form
      {
          uint handle;
          static WriteLog log = new WriteLog();
          Boolean getCan = false;
          static int frameCount = 0;
          static CANMsg newmsg = new CANMsg();
          callBack _setCallBack;
          List&lt;string&gt; write = new List&lt;string&gt;();
  
          public Form1()
          {
              InitializeComponent();
          }
  
  
          private void buttonOpen_Click(object sender, EventArgs e)
          {
                  // Open connection
          }
  
          private void buttonClose_Click(object sender, EventArgs e)
          {
                 // Close connection
          }
  
          private void buttonCallBack_Click(object sender, EventArgs e)
          {
              if (!getCan)
              {
                  int rv;
                  unsafe
                  {
                      callBack _setCallBack = new callBack(call);
                      rv = canusb_setReceiveCallBack(handle, _setCallBack);
                  }
                  label1.Text = rv.ToString();
              }
              else
              {
                  _setCallBack = null;
                  int rv = canusb_setReceiveCallBack(handle, _setCallBack);
                  GC.KeepAlive(_setCallBack);
                  label1.Text = rv.ToString();
              }
          }
  
          public unsafe void call(CANMsg *pmsg)
          {
              newmsg = *pmsg;
              update();
          }
  
          private void buttonExit_Click(object sender, EventArgs e)
          {
              GC.KeepAlive(_setCallBack);
              Application.Exit();
          }
  
          [DllImport("canusbdrv.dll", EntryPoint = "canusb_setReceiveCallBack")]
          public static extern int canusb_setReceiveCallBack(uint handle, callBack callBack);
  
          unsafe private void timer_Tick(object sender, EventArgs e)
          {
                // update the form with received messages
          }
  
          public void update()
          {
              CANMsg msgrec = newmsg;
              // Build str from messages with all data
              write.Add(str);
              log.logWrite(str);
              frameCount++;
          }
      }
  
      public class WriteLog
      {
  
          private void OpenFile()
          {     }
  
          public void logWrite(string log)
          {     }
  
          public void logAdd(string log)
          {     }
  
          private void logClose()
          {     }
      }
  }
  </code></pre>