4

As I saw in Windows 8 for .NET developers session .NET deals with native objects of the platform. And these objects support Pointer Counters. It was clear for me that there were managed objects which sits in the managed heap and unmanaged which we had to release in the finalize and dispose methods. As for now it's not clear for me how should I deal with memory management in Windows Runtime. Can you give me an advice when I have to worry about managed/unmanaged resources? And how does memory management work in Windows Runtime in order to prevent my apps from resource leaking?

UPD:

I mean .NET Metro Profile

Access Denied
  • 8,723
  • 4
  • 42
  • 72

1 Answers1

4

The underlying WinRT interface is COM based. Every WinRT interface derives from IUnknown, its AddRef and Release methods implement reference count based memory management.

This is however very well hidden, when you program a WinRT app then you use a language projection. For .NET and Javascript apps, the language projection is built into, respectively, the CLR and the Chakra engine. Which complete hide the implementation details, the WinRT interfaces are mapped to native language constructs. Including skillfully creating the illusion that COM supports generics and implementation inheritance. A somewhat reasonable guess is that the CLR projection uses COM interop support already built into the CLR but it is impossible to reverse-engineer this easily. If that's accurate at all then finalizers are likely to make the IUnknown::Release() call.

Right now it is very hard to get decent info about the low-level details, the Microsoft bloggers and SO posters are not talking, source code is not available and questions that touch on the subject are getting closed, like this one.

As you can tell from Chen's comment to that question, you are not supposed to be curious about it. If you are worried then consider programming in C++/CX. Which does a good job of hiding the glue. Or native C++ with the WRL library, which doesn't. Both runtime environments where memory management is explicit. Microsoft invested a lot of resources in making C++ programming relevant again.

Community
  • 1
  • 1
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • Many thanks for your answer. As far as I know C# was designed exclusively for .NET. And what about Metro? Does it mean that if I start developing my first Metro application I'd better choose C++ or I should not take care about projections? Are there are any known Memory Leaks in Windows Runtime? – Access Denied Sep 01 '12 at 16:35
  • I've read thread mentioned above and didn't get strong opinion regarding best projection. Seems like all of them have pros and cons. – Access Denied Sep 01 '12 at 16:57
  • 1
    One important aspect of WinRT programming (that came in fairly late in the development cycle) is that many winrt objects now support ICloseable, which is projected to C# apps as IDisposable. This allows .Net and JS applications to implement deterministic finalization. – Larry Osterman Sep 02 '12 at 00:18