Warning .. long post.
Basically the CLR generates a thunk that it passes out to the
unmanaged code (this thunk does not move). When called back the thunk
then indirectly calls the delegate (it knows if the delegate moves due
to GC). Stephen Toub gave a nice description of this process in MSDN
Magazine (May 2006) but for some reason the current version on MSDN is
missing this part here is a link to google's cache that includes the
information
http://64.233.187.104/search?q=cache:ABTJ5ynuh0IJ:msdn.microsoft.com/msdnmag/issues/06/05/BugBash/
its under the section dealing with the CallbackOnCollectedDelegate MDA
(also pay attention to what happens if your delegate gets collected!!)
I will put all of this into a blog post (in a more clear way) but here
is a bit to show you that this _is_ actually happenning :)
I hacked together this code quickly off of the internet .. it is just
a copy/paste .. dont expect much but I needed some test code.
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace ConsoleApplication35 {
using System;
class HandleControlSignals {
[DllImport("Kernel32")]
public static extern bool SetConsoleCtrlHandler(HandlerRoutine
Handler, bool Add);
// A delegate type to be used as the handler routine
// for SetConsoleCtrlHandler.
public delegate bool HandlerRoutine(CtrlTypes CtrlType);
// An enumerated type for the control messages
// sent to the handler routine.
public enum CtrlTypes {
CTRL_C_EVENT = 0,
CTRL_BREAK_EVENT,
CTRL_CLOSE_EVENT,
CTRL_LOGOFF_EVENT = 5,
CTRL_SHUTDOWN_EVENT
}
private static bool ConsoleCtrlCheck(CtrlTypes ctrlType) {
Console.WriteLine(ctrlType);
return true;
}
public static void Main() {
SetConsoleCtrlHandler(new HandlerRoutine(ConsoleCtrlCheck), true);
Console.WriteLine("Press 'Q' (followed by Enter) to quit.");
while ((Console.ReadLine() != "Q")) {
}
SetConsoleCtrlHandler(null, false);
}
}
}
Follow instructions here to enable unmanaged debugging with JIT
optimizations
http://codebetter.com/blogs/gregyoung/archive/2006/06/09/146298.aspx
Set a break point in the callback method and run ...
when breakpoint hit .. in immediate window.
.load SOS
extension C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\SOS.dll loaded
!ClrStack
OS Thread Id: 0xc90 (3216)
ESP EIP
00eafc4c 00de0104
ConsoleApplication35.HandleControlSignals.ConsoleCtrlCheck(CtrlTypes)
00eafd78 79e88f63 [GCFrame: 00eafd78]
00eaff18 79e88f63 [UMThkCallFrame: 00eaff18]
We can see from our call stack that we have come in through a UMThunk
(the thunk generated by the JIT).
Cheers,
Greg Young
On 7/9/06, John Whattam <[EMAIL PROTECTED]> wrote:
I have read up on all I can regarding delegates in NET. All documentation
refers to them as being type-safe pointers. As such they hold a pointer to
a memory address. I am using a delegate as a pointer to a callback for the
EnumFontFamiliesEx API. If the garbage collector collects objects that are
completely unrelated to the object holding the delegate and thus
rearranges objects in memory, is the address pointed to by the delegate
updated to reflect the new memory location of the callback function or
does it still point to the original memory location specified when the
delegate was instantiated?
Regards,
John Whattam.
===================================
This list is hosted by DevelopMentor(r) http://www.develop.com
View archives and manage your subscription(s) at http://discuss.develop.com
--
If knowledge can create problems, it is not through ignorance that we
can solve them.
Isaac Asimov
===================================
This list is hosted by DevelopMentorĀ® http://www.develop.com
View archives and manage your subscription(s) at http://discuss.develop.com