Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. I am geting an error about callbackOnCollectedDelegate

I am geting an error about callbackOnCollectedDelegate

Scheduled Pinned Locked Moved C#
sysadminhelp
4 Posts 3 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mahua22
    wrote on last edited by
    #1

    I'm getting the folloiwng error: A callback was made on a garbage collected delegate of type 'OPC_Library!OPC_Library.OPC_DLL+WriteNotificationDelegate::Invoke'. This may cause application crashes, corruption and data loss. When passing delegates to unmanaged code, they must be kept alive by the managed application until it is guaranteed that they will never be called. I m creating an opc server,I have a form opcSrvr & a .cs file called TcpCommServer, my code is here: opcSrvr.cs OPC_DLL.WriteNotificationDelegate writeCallBack; public static TcpCommServer.ServerCallbackDelegate del; TcpCommServer _Server; public opcSrvr() { InitializeComponent(); writeCallBack = new OPC_DLL.WriteNotificationDelegate(myWriteCallBack); del += new Airsprint_OPCServer.TcpCommServer.ServerCallbackDelegate(UpdateUI); _Server = new Airsprint_OPCServer.TcpCommServer(del); } public void UpdateUI(byte[] bytes, Int32 sessionID, int dataChannel) { if (this.InvokeRequired) { this.Invoke(_Server.ServerCallbackObject, bytes, sessionID, dataChannel); } else { // the usual stuff } } public void myWriteCallBack(UInt32 hItem, ref Object Value, ref UInt32 ResultCode) { ResultCode = 0; try { ResultCode = 0; if (hItem == TagHandle[0]) { textBox1.Text = Convert.ToString (Value); OPC_DLL.UpdateTag.UpdateTag(TagHandle[0], textBox1.Text, 192); } if (hItem == TagHandle[1]) { textBox2.Text = Convert.ToString(Value); OPC_DLL.UpdateTag.UpdateTag(TagHandle[1], textBox2.Text, 192); } if (hItem == TagHandle[2]) { textBox3.Text = Convert.ToString(Value); OPC_DLL.UpdateTag.UpdateTag(TagHandle[2], textBox3.Text, 192); } if (hItem == TagHandle[3]) { textBox4.Text = Convert.ToString(Value); OPC_DLL.UpdateTag.UpdateTag(TagHandle[3], textBox4.Text, 192); } } catch(Exception ex) { } } private void opcSrvr_Load(object sender, EventArgs e) { OPC_DLL.I

    R A 2 Replies Last reply
    0
    • M mahua22

      I'm getting the folloiwng error: A callback was made on a garbage collected delegate of type 'OPC_Library!OPC_Library.OPC_DLL+WriteNotificationDelegate::Invoke'. This may cause application crashes, corruption and data loss. When passing delegates to unmanaged code, they must be kept alive by the managed application until it is guaranteed that they will never be called. I m creating an opc server,I have a form opcSrvr & a .cs file called TcpCommServer, my code is here: opcSrvr.cs OPC_DLL.WriteNotificationDelegate writeCallBack; public static TcpCommServer.ServerCallbackDelegate del; TcpCommServer _Server; public opcSrvr() { InitializeComponent(); writeCallBack = new OPC_DLL.WriteNotificationDelegate(myWriteCallBack); del += new Airsprint_OPCServer.TcpCommServer.ServerCallbackDelegate(UpdateUI); _Server = new Airsprint_OPCServer.TcpCommServer(del); } public void UpdateUI(byte[] bytes, Int32 sessionID, int dataChannel) { if (this.InvokeRequired) { this.Invoke(_Server.ServerCallbackObject, bytes, sessionID, dataChannel); } else { // the usual stuff } } public void myWriteCallBack(UInt32 hItem, ref Object Value, ref UInt32 ResultCode) { ResultCode = 0; try { ResultCode = 0; if (hItem == TagHandle[0]) { textBox1.Text = Convert.ToString (Value); OPC_DLL.UpdateTag.UpdateTag(TagHandle[0], textBox1.Text, 192); } if (hItem == TagHandle[1]) { textBox2.Text = Convert.ToString(Value); OPC_DLL.UpdateTag.UpdateTag(TagHandle[1], textBox2.Text, 192); } if (hItem == TagHandle[2]) { textBox3.Text = Convert.ToString(Value); OPC_DLL.UpdateTag.UpdateTag(TagHandle[2], textBox3.Text, 192); } if (hItem == TagHandle[3]) { textBox4.Text = Convert.ToString(Value); OPC_DLL.UpdateTag.UpdateTag(TagHandle[3], textBox4.Text, 192); } } catch(Exception ex) { } } private void opcSrvr_Load(object sender, EventArgs e) { OPC_DLL.I

      R Offline
      R Offline
      Ron Beyer
      wrote on last edited by
      #2

      Does this happen when the OPC server is closed, or the application exits? You should properly implement IDisposable on your OPC type so that you can release the event handlers correctly. This requires you to implement a Dispose() method where you can release the server callback delegates. What is probably happening is that the server isn't referenced anymore so the garbage collector picks it up and collects it, but since the OPC server still has active delegates it tries to call them on a disposed object. You need to release the OPC COM objects and un-initialize the OPC server when the server gets garbage collected.

      1 Reply Last reply
      0
      • M mahua22

        I'm getting the folloiwng error: A callback was made on a garbage collected delegate of type 'OPC_Library!OPC_Library.OPC_DLL+WriteNotificationDelegate::Invoke'. This may cause application crashes, corruption and data loss. When passing delegates to unmanaged code, they must be kept alive by the managed application until it is guaranteed that they will never be called. I m creating an opc server,I have a form opcSrvr & a .cs file called TcpCommServer, my code is here: opcSrvr.cs OPC_DLL.WriteNotificationDelegate writeCallBack; public static TcpCommServer.ServerCallbackDelegate del; TcpCommServer _Server; public opcSrvr() { InitializeComponent(); writeCallBack = new OPC_DLL.WriteNotificationDelegate(myWriteCallBack); del += new Airsprint_OPCServer.TcpCommServer.ServerCallbackDelegate(UpdateUI); _Server = new Airsprint_OPCServer.TcpCommServer(del); } public void UpdateUI(byte[] bytes, Int32 sessionID, int dataChannel) { if (this.InvokeRequired) { this.Invoke(_Server.ServerCallbackObject, bytes, sessionID, dataChannel); } else { // the usual stuff } } public void myWriteCallBack(UInt32 hItem, ref Object Value, ref UInt32 ResultCode) { ResultCode = 0; try { ResultCode = 0; if (hItem == TagHandle[0]) { textBox1.Text = Convert.ToString (Value); OPC_DLL.UpdateTag.UpdateTag(TagHandle[0], textBox1.Text, 192); } if (hItem == TagHandle[1]) { textBox2.Text = Convert.ToString(Value); OPC_DLL.UpdateTag.UpdateTag(TagHandle[1], textBox2.Text, 192); } if (hItem == TagHandle[2]) { textBox3.Text = Convert.ToString(Value); OPC_DLL.UpdateTag.UpdateTag(TagHandle[2], textBox3.Text, 192); } if (hItem == TagHandle[3]) { textBox4.Text = Convert.ToString(Value); OPC_DLL.UpdateTag.UpdateTag(TagHandle[3], textBox4.Text, 192); } } catch(Exception ex) { } } private void opcSrvr_Load(object sender, EventArgs e) { OPC_DLL.I

        A Offline
        A Offline
        Alan N
        wrote on last edited by
        #3

        It looks like you knew what you were supposed to do but then forgot to do it. In the constructor you have explicitly created a delegate and assigned it to the field writeCallBack so that it will not be garbage collected. But in the opcSrvr_Load method there is implicit delegate creation in the line OPC_DLL.EnableWriteNotification(myWriteCallBack, true); Should that be OPC_DLL.EnableWriteNotification(this.writeCallBack, true); Alan.

        R 1 Reply Last reply
        0
        • A Alan N

          It looks like you knew what you were supposed to do but then forgot to do it. In the constructor you have explicitly created a delegate and assigned it to the field writeCallBack so that it will not be garbage collected. But in the opcSrvr_Load method there is implicit delegate creation in the line OPC_DLL.EnableWriteNotification(myWriteCallBack, true); Should that be OPC_DLL.EnableWriteNotification(this.writeCallBack, true); Alan.

          R Offline
          R Offline
          Ron Beyer
          wrote on last edited by
          #4

          Good catch, still needs to implement proper disposal methods though when working with COM objects like OPC servers :)

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups