CallbackOnCollectedDelegate was detected

  • Thread starter Wilfried Mestdagh [MapPoint MVP]
  • Start date
W

Wilfried Mestdagh [MapPoint MVP]

Hi,

I have sometime following error:
CallbackOnCollectedDelegate was detected
Message: A callback was made on a garbage collected delegate of type
'SmsOutDBG!SmsOutDBG.Program+HandlerRoutine::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.

What I do is start a thread in a console application like this:
static void Main(string[] args)
{
SetConsoleCtrlHandler(new HandlerRoutine(ConsoleCtrlCheck), true);

App app = new App();
appThread = new Thread(new ThreadStart(app.Run));
appThread.Start();

do {
ConsoleKeyInfo key = Console.ReadKey(true);

and detect the Control+C to terminate where I stop the thread like this:
private static bool ConsoleCtrlCheck(CtrlTypes ctrlType)
{
appThread.Abort();
appThread.Join();
return false;
}

and the thread is only this:
public void Run()
{
try {
Console.WriteLine("Application thread started");
while (true) {
Thread.Sleep(1000);
}
} catch(ThreadAbortException) {
Thread.ResetAbort();
}
// Todo: Do cleanup here
}

The exception error shows up in the catch part!. Can someone tell what could
be wrong here?
 
W

Wilfried Mestdagh [MapPoint MVP]

Hello,

It looks that if I keep a pointer during lifetime of the application to the
control C handler that it is solved. Like this:

static private HandlerRoutine ctrlCHandler;

static void Main(string[] args)
{
ctrlCHandler = new HandlerRoutine(ConsoleCtrlCheck);
SetConsoleCtrlHandler(ctrlCHandler, true);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top