Joe Beda - Managed vs. unmanaged, how much of Avalon was done that way?
- Posted: Apr 09, 2004 at 1:49 PM
- 13,249 Views
- 3 Comments
Loading User Information from Channel 9
Something went wrong getting user information from Channel 9
Loading User Information from MSDN
Something went wrong getting user information from MSDN
Loading Visual Studio Achievements
Something went wrong getting the Visual Studio Achievements
Right click “Save as…”
Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation,
please create a new thread in our Forums,
or
Contact Us and let us know.
Follow the Discussion
Oops, something didn't work.
What does this mean?
Following an item on Channel 9 allows you to watch for new content and comments that you are interested in. You need to be signed in to Channel 9 to use this feature.What does this mean?
Following an item on Channel 9 allows you to watch for new content and comments that you are interested in and view them all on your notifications page.sign up for email notifications?
I can speak to this a little bit. Originally, we didn't know if we were going to have to deliver an unmanaged API to go along with our managed API. To do this, we implemented our visual (basically a node in our display tree) in unmanaged code with managed wrappers. We hit real problems with lifetime management when we wanted to attach any sort of managed data to our unmanaged objects. This happened when we let users attach data to the visuals (think SetWindowPtr) or try to call back into managed code (think WM_PAINT). WinForms handles this type of thing (I think) with hash tables and explicit lifetime management. Neither of those is efficient enought or clean enough for where we wanted to go with Avalon.
The basic problem is that if you have a well connected object graph, it doesn't work well split across the managed/unmanaged boundary. The simple way to do this is to have everything that unmanaged code is holding on to be a GC root. This means that the GC won't see through unmanaged code to collect the managed object held on to by managed code. To put it in concrete terms, suppose you have a managed object A (Am) that is holding on to an unmanaged object B (Bu). No suppose that Bu is holding on to a managed object C (Cm). Now suppose (directly or indirectly) Cm is holding on to Am. In this situation we have a reference loop that goes through unmanaged code. Even if there are no external references holding on to any of these objects, the GC will never know to collect them as it can't trace the indirect reference from Am to Cm.
The upshot is that you really want the transition layer to be as "one way" as possible. APIs like GDI+ (System.Drawing) or DX work well because they rarely call back or hold on to managed user data. APIs like WinForms (which is, amoung other things, a wrapper on USER) have to bend over backwards to straddle the line like they do.
Internally, we moved a bunch more stuff over to the managed side so that we could make our API be more "one way".
Remove this comment
Remove this thread
close