473,796 Members | 2,464 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

callback on a garbage collected delegate error

Hi,

I have an application using a DLL and callbacks. It generate random the
error "A callback was made on a garbage collected delegate".

I found some articles that the pointer to the delegate has to have a
lifetime reference so that it is not garbage collected. But I could not find
any example how to do this.

Can someone help here ?

--
rgds, Wilfried
http://www.mestdagh.biz
Sep 25 '06 #1
13 14563
Wilfried,

Just assign the delegate reference to a field in a class that has the
same lifetime as the callback. So if you have a class instance which will
receive the callback, then assign the delegate to a field on the class.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Wilfried Mestdagh" <Wi************ **@discussions. microsoft.comwr ote in
message news:8A******** *************** ***********@mic rosoft.com...
Hi,

I have an application using a DLL and callbacks. It generate random the
error "A callback was made on a garbage collected delegate".

I found some articles that the pointer to the delegate has to have a
lifetime reference so that it is not garbage collected. But I could not
find
any example how to do this.

Can someone help here ?

--
rgds, Wilfried
http://www.mestdagh.biz

Sep 25 '06 #2
Hi,

I understeand but I dont understeand how and where.

I have following:

public class Api
{
public Api(Control control)
{
owner = control;
ApiDll.Start();
ApiDll.SetLogge dOn(cbLoggedOn) ;
// cb... is a callback function in this class
// Set LoggedOn is the procedure to place the pointer in the DLL
// etc...

Then in main form:

public partial class Main : Form
{
private Api api;

public Main()
{
InitializeCompo nent();
api = new Api(this);
api.OnLoggedOn += api_OnLoggedOn;
// etc..

then in the DLL declaration:

public static class ApiDll
{
private const string dllName = "SmsComfortAPI. dll";

public delegate void OnLoggedOn();
// etc...

[DllImport(dllNa me)]
public static extern void SetLoggedOn(OnL oggedOn cbLoggedOn);
// etc...
Where and how should I declare the lifetime inctance ?

--
rgds, Wilfried
http://www.mestdagh.biz
Sep 25 '06 #3
Wilfried,

Where are you getting cb from? If it is a member in your Api class,
then the problem is that the class is being collected (not just the
delegate).

You need to call a function to unregister the callback so that it
doesn't try and call your function back. Either that, or you need to have a
static method and pass a delegate to that, and then register with the class
for the callback.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Wilfried Mestdagh" <Wi************ **@discussions. microsoft.comwr ote in
message news:A0******** *************** ***********@mic rosoft.com...
Hi,

I understeand but I dont understeand how and where.

I have following:

public class Api
{
public Api(Control control)
{
owner = control;
ApiDll.Start();
ApiDll.SetLogge dOn(cbLoggedOn) ;
// cb... is a callback function in this class
// Set LoggedOn is the procedure to place the pointer in the
DLL
// etc...

Then in main form:

public partial class Main : Form
{
private Api api;

public Main()
{
InitializeCompo nent();
api = new Api(this);
api.OnLoggedOn += api_OnLoggedOn;
// etc..

then in the DLL declaration:

public static class ApiDll
{
private const string dllName = "SmsComfortAPI. dll";

public delegate void OnLoggedOn();
// etc...

[DllImport(dllNa me)]
public static extern void SetLoggedOn(OnL oggedOn cbLoggedOn);
// etc...
Where and how should I declare the lifetime inctance ?

--
rgds, Wilfried
http://www.mestdagh.biz

Sep 25 '06 #4
Hi Nicholas,
Where are you getting cb from? If it is a member in your Api class,
then the problem is that the class is being collected (not just the
delegate).
the cb... functions are indeed a members of the Api class.
You need to call a function to unregister the callback so that it
doesn't try and call your function back. Either that, or you need to have a
static method and pass a delegate to that, and then register with the class
for the callback.
I dont understeand. English is not my native language eather. How do I make
a function to uregister it ? But if it is unregistered then how will the
callback works ?

The project load a DLL. DLL communicate with a server trough a TCP session.
The callback is needed to get data from server. So I dont understeand the
unregistering.. .

Should I make a simple project to demonstrate and publish it for download ?

--
rgds, Wilfried
http://www.mestdagh.biz
Sep 25 '06 #5
Wilifried,

You have an API function SetLoggedOn, then it is storing in that dll the
address of the callback function. When someone logs on, it is calling the
callback function.

You have to tell it to stop calling that callback, since your instance
goes out of scope.

What I think you should do is make this event static, and then make your
delegate to a static method.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m
"Wilfried Mestdagh" <Wi************ **@discussions. microsoft.comwr ote in
message news:40******** *************** ***********@mic rosoft.com...
Hi Nicholas,
> Where are you getting cb from? If it is a member in your Api class,
then the problem is that the class is being collected (not just the
delegate).

the cb... functions are indeed a members of the Api class.
> You need to call a function to unregister the callback so that it
doesn't try and call your function back. Either that, or you need to
have a
static method and pass a delegate to that, and then register with the
class
for the callback.

I dont understeand. English is not my native language eather. How do I
make
a function to uregister it ? But if it is unregistered then how will the
callback works ?

The project load a DLL. DLL communicate with a server trough a TCP
session.
The callback is needed to get data from server. So I dont understeand the
unregistering.. .

Should I make a simple project to demonstrate and publish it for download
?

--
rgds, Wilfried
http://www.mestdagh.biz

Sep 25 '06 #6
Hi Nicholas,
You have an API function SetLoggedOn, then it is storing in that dll the
address of the callback function. When someone logs on, it is calling the
callback function.
SetLoggedOn installs the callback pointer into the DLL. The DLL calls back
to the pointer when the application is logged on to the server.
You have to tell it to stop calling that callback, since your instance
goes out of scope.
But the DLL needs the callback and other callback for many other events. The
DLL comunicate with a server and data can arrive.
What I think you should do is make this event static, and then make your
delegate to a static method.
I think I tryed already to make the procedure static with same result. But I
tryed in the last hours so many things I'm not sure anymore (including
banging my head on keyboard and monitore)...

--
rgds, Wilfried
http://www.mestdagh.biz
Sep 25 '06 #7

"Wilfried Mestdagh" <Wi************ **@discussions. microsoft.comwr ote in
message news:87******** *************** ***********@mic rosoft.com...
| Hi Nicholas,
|
| You have an API function SetLoggedOn, then it is storing in that dll
the
| address of the callback function. When someone logs on, it is calling
the
| callback function.
|
| SetLoggedOn installs the callback pointer into the DLL. The DLL calls back
| to the pointer when the application is logged on to the server.
|
| You have to tell it to stop calling that callback, since your
instance
| goes out of scope.
|
| But the DLL needs the callback and other callback for many other events.
The
| DLL comunicate with a server and data can arrive.
|
| What I think you should do is make this event static, and then make
your
| delegate to a static method.
|
| I think I tryed already to make the procedure static with same result. But
I
| tryed in the last hours so many things I'm not sure anymore (including
| banging my head on keyboard and monitore)...
|
| --
| rgds, Wilfried
| http://www.mestdagh.biz

We have to see how and where you create an instance of your delegate, so,
please post a short but complete repro that illustrates your issue.
incomplete code snips like you posted aren't of great help.
Willy.
Sep 25 '06 #8
Hi,

Yes you are right of course. I made a simple application to demonstrate my
problem. It is containing only the nececary. I also made source for download
if that is more easy on http://www.mestdagh.biz/kieken/CrashTest.zip The dll
I made for the demo is just calling the callback with the same data. this is
the complete project. Using the garbage collector I can let it reproduce the
error 1 on 2 times.

using System;
using System.Windows. Forms;
using System.Runtime. InteropServices ;

namespace DllCrashDemo
{
public partial class Form1 : Form
{
private Api api;

public Form1()
{
InitializeCompo nent();
api = new Api(this);
api.OnShow += api_OnShow;
}

void api_OnShow(obje ct sender, Api.ShowItArgs e)
{
listBox.Items.A dd(e.text);
}

private void crashButton_Cli ck(object sender, EventArgs e)
{
api.show("This is a crashtest");
}
}

public class Api
{
private Control owner;

public Api(Control control)
{
owner = control;
ApiDll.SetShow( cbShow);
}

public void show(string text)
{
ApiDll.ShowIt(t ext);
GC.Collect();
}

public class ShowItArgs : EventArgs
{
public string text;
}
public delegate void Show(object sender, ShowItArgs e);
public event Show OnShow;
private void cbShow(string text)
{
if (OnShow == null)
return;
ShowItArgs e = new ShowItArgs();
e.text = text;
owner.Invoke(On Show, new object[] { this, e });
}
}

public static class ApiDll
{
private const string crashDllName = @"C:\Program
Files\Borland\D elphi7\Projects \CrashDll.dll";

public delegate void OnShow([MarshalAs(Unman agedType.LPStr)]string
txt);

[DllImport(crash DllName)]
public static extern void
ShowIt([MarshalAs(Unman agedType.LPStr)]string txt);
[DllImport(crash DllName)]
public static extern void SetShow(OnShow cbShow);
}
}

--
rgds, Wilfried
http://www.mestdagh.biz
"Willy Denoyette [MVP]" wrote:
>
"Wilfried Mestdagh" <Wi************ **@discussions. microsoft.comwr ote in
message news:87******** *************** ***********@mic rosoft.com...
| Hi Nicholas,
|
| You have an API function SetLoggedOn, then it is storing in that dll
the
| address of the callback function. When someone logs on, it is calling
the
| callback function.
|
| SetLoggedOn installs the callback pointer into the DLL. The DLL calls back
| to the pointer when the application is logged on to the server.
|
| You have to tell it to stop calling that callback, since your
instance
| goes out of scope.
|
| But the DLL needs the callback and other callback for many other events.
The
| DLL comunicate with a server and data can arrive.
|
| What I think you should do is make this event static, and then make
your
| delegate to a static method.
|
| I think I tryed already to make the procedure static with same result. But
I
| tryed in the last hours so many things I'm not sure anymore (including
| banging my head on keyboard and monitore)...
|
| --
| rgds, Wilfried
| http://www.mestdagh.biz

We have to see how and where you create an instance of your delegate, so,
please post a short but complete repro that illustrates your issue.
incomplete code snips like you posted aren't of great help.
Willy.
Sep 26 '06 #9
Wilfried Mestdagh <Wi************ **@discussions. microsoft.comwr ote:
Yes you are right of course. I made a simple application to demonstrate my
problem. It is containing only the nececary. I also made source for download
if that is more easy on http://www.mestdagh.biz/kieken/CrashTest.zip The dll
I made for the demo is just calling the callback with the same data. this is
the complete project. Using the garbage collector I can let it reproduce the
error 1 on 2 times.
Ah - I don't know whether it's because I missed something before or
not, but I hadn't realised you were talking about unmanaged code
calling you back. That explains why the garbage collector was able to
collect the delegate in the first place.

There are various ways you could handle this, but the main thing is
that you'll need to keep a reference to the delegate within managed
code.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 26 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
12955
by: Martin Maat | last post by:
I am puzzled. I have this object that uses a thread. The thread is encapsulated by the object, the object has Start and Stop methods to enable the client to start or stop the thread. I found that the object will not be garbage collected while the thread is running. Fair enough, the documented explanation is that the GC compresses objects on the heap and needs to update references, the references will be invalid for a couple of moments...
4
2404
by: Sai Kit Tong | last post by:
I have to interface managed application with my legacy dll. I have employed the wrapper approach but I have to deal with the asynchronous callback from the legacy dll, which likely goes through a thread other than the initial calling thread. I got the idea from MSDN and other articles on using the delegate. However, for garabage collection issue, I need to pin the delegate. Since my callback is asynchronous, I have been thinking about...
9
2193
by: Olivier Fermy | last post by:
I have created a sample project where i have referenced an object only with an event : textBox.VisibleChanged += new EventHandler(this.textBox_VisibleChanged); When i call GC.Collect(), the object is disposed and garbage collected ! I thought that event references are strong references and not weak references. Does the .NET 2.0 garbage collector manage differently the events references ?
6
2407
by: Minfu Lu | last post by:
I have a problem dealing with passing a function address to a COM callback. I use this COM function for communicating to a hardware. My original project was written in VB. I have converted it to C#. One of the problem is passing a function address to a COM function as a parameter with another progress value. My callback function is very simple using the progress value to update my progressbar. Because this COM function usually takes a long...
4
1989
by: FishingScout | last post by:
I am re-writing an MS VC++ 6.0 application in Visual Studio 2005 VB.NET. In order for my new application to communicate with some hardware (an RFID reader) I need to communicate with a DLL that was written in MS VC++ 6.0. I have found some excellent discussions that have helped me define the structures to marshal the data between the unmanaged and managed code. My problem is that my application is not working correctly. The result is...
4
1699
by: R. MacDonald | last post by:
Hello, all, I have a .NET application (VB) that passes the address of a delegate to unmanaged code in a DLL. The unmanaged code then uses the delegate as a call-back. This seems to work fine, but now I am worried about garbage collection. I am concerned that the location of the delegate might be altered as a result of other (unused) objects being garbage collected. This would probably cause undesirable results when the unmanaged DLL...
6
14089
by: Bart Burkhardt | last post by:
Hi, I could use some help in setting a C# callback function that an external unmanaged dll will call on a event. Using a delegate and use the external callback set function doesn't work. The carbage collector says hello here (-; I have pasted some code below, any help is greatly appreciated! //Bart
4
4802
by: Edwin Gomez | last post by:
I'm a C# developer and I'm new to Python. I would like to know if the concept of Asynchronous call-backs exists in Python. Basically what I mean is that I dispatch a thread and when the thread completes it invokes a method from the calling thread. Sort event driven concept with threads. Thanks. Ed Gomez
10
7001
by: SQACPP | last post by:
Hi, I try to figure out how to use Callback procedure in a C++ form project The following code *work* perfectly on a console project #include "Windows.h" BOOL CALLBACK MyEnumWindowsProc(HWND hwnd, LPARAM lparam) {
0
9680
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10228
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10006
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9052
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7547
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6788
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5441
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4116
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3731
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.