2

If not, then does WinRT have its own Garbage Collector?

I ask this because I read this: "There's no need to manage the lifetime of underlying object. Windows releases the object when you're finished with the last of its class instances that you've activated." from MSDN.

Kate Gregory
  • 18,808
  • 8
  • 56
  • 85
Cui Pengfei 崔鹏飞
  • 8,017
  • 6
  • 46
  • 87

1 Answers1

5

They don't. WinRT doesn't use a garbage collector. Memory is managed with reference counting, IUnknown::AddRef() and IUnknown::Release(). Just like COM. And no, it isn't Windows that takes care of the counting, it is the language runtime support library. Javascript always used reference counting, C++ gets it from the C++/CX language extensions or by using smart pointer classes.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • 1
    I'm pretty sure JavaScript (including the JScript flavor) does not use reference counting, at least not for non-COM (and I suppose non-WinRT) JavaScript objects. – Bob77 Oct 12 '11 at 14:03
  • Indeed, for the curious the JavaScript engine in Metro Style JS apps interacts with three different memory systems: 1) Pure JavaScript objects which are garbage collected and managed within the engine. 2) Host provides objects such as the DOM (HTML elements etc.) which historically were ref counted COM objects and managed via Add/Release which made them prone to leaks ( – Andy Dec 02 '11 at 22:37
  • 3) WinRT (and COM) objects which are all referenced counted, but from JavaScript code the engine manages adding and releasing so a JS developer doesn't need to worry about it. However, the actual COM/WinRT object is referenced counted, it just so happens that in the WinRT all the callers implement systems either in their garbage collector (managed and JS) or in the language extensions (C++) to make it transparent to the developer. – Andy Dec 02 '11 at 22:37