I am geting an error about callbackOnCollectedDelegate
-
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
-
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
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.
-
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
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 beOPC_DLL.EnableWriteNotification(this.writeCallBack, true);
Alan. -
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 beOPC_DLL.EnableWriteNotification(this.writeCallBack, true);
Alan.