{"id":442,"date":"2011-09-17T04:45:04","date_gmt":"2011-09-17T04:45:04","guid":{"rendered":"http:\/\/www.themissingdocs.net\/wordpress\/?p=442"},"modified":"2011-09-17T04:45:04","modified_gmt":"2011-09-17T04:45:04","slug":"winrt-and-the-return-of-the-memory-leak","status":"publish","type":"post","link":"https:\/\/www.themissingdocs.net\/?p=442","title":{"rendered":"WinRT and the return of the memory leak&#8230;"},"content":{"rendered":"<p>WinRT, so much new and interesting&#8230; so much old and broken?\u00a0 I&#8217;ve read a few articles now, watched a couple of talk recordings &#8211; nothing great and in-depth but I think I&#8217;ve picked up a few details.<\/p>\n<p>WinRT is effectively the combination of &#8216;COM done better&#8217; and &#8216;OS API done using the result&#8217;.<\/p>\n<p>On the COM done better side we get, nicer integration into more languages.\u00a0 Along with that is a metadata format which is shipped with the code.\u00a0 Metadata which includes versioning information to try and avoid some of the COM interface versioning hell.\u00a0 You get better runtime reflection &#8211; every WinRT type can tell you what interfaces it implements and provide some kind of name.\u00a0 Apparently there is type inheritance (although apparently only the WinRT core is allowed to use it&#8230;), and generics and better handling of events and asynchronous callbacks.\u00a0 A new better type of string, simple structs and collection types built into the core that everyone can consistently use as part of their own APIs.\u00a0 Each app apparently even has its own COM(I mean WinRT) registry, so no conflict issues.<\/p>\n<p>OS API done using the result gives opportunities to do things the way they should be done.\u00a0 It also allows for namespacing and componentization, which when mixed with the metadata mean much improved discoverability and general developer experience.\u00a0 No need to call clean-up methods, because they are automatically called when the associated type is released&#8230;<\/p>\n<p>Which brings us to the &#8216;old and broken&#8217; part of this story.\u00a0 WinRT shows its COM roots, everything is IUnknown, the same IUnknown from COM.\u00a0 This means AddRef and Release.\u00a0 The real bonus here however is that this is all hidden away from c#\/VB\/javascript and even c++ if you use the c++\/cx extension approach to dealing with WinRT.\u00a0 I say bonus somewhat dubiously as fully-automatic reference counting results in fully-automatic memory leaks.<\/p>\n<p>Take for example the PropertySet in WinRT.\u00a0 Using this from c# in VS11 the only vaguely apparent suggestion that this isn&#8217;t a .Net type is its namespace.\u00a0 I can easily see developers thinking that this was a replacement for StringDictionary (which has conveniently been removed from the .Net 4.5 core runtime).\u00a0 But you will see the difference if you create a circle of references between PropertySets.\u00a0 You have to manually break the circle or the PropertySet memory will never be released!<\/p>\n<p>As a bonus to this post, I&#8217;ll end by mentioning that WinRT apparently does not support exceptions!\u00a0 This may seem strange to anyone who has used it, since calling WinRT APIs certainly throws exceptions, but this is entirely language integration, under the hood an error is limited to a 32bit integer &#8211; a HRESULT.\u00a0 This means WinRT components cannot provide useful error details directly through their APIs without mangling those APIs to provide a bunch of output parameters that are normally never used.<\/p>\n<p>I was watching one of the WinRT talks where they decided to describe a &#8216;feature&#8217; that if an error occurs in the WinRT core libraries, it can directly tell the attached debugger additional details, since they cannot be returned to the calling code! (One can only hope that they provide a way for custom written components to do this too, it is going to be bad enough that when ComponentArt 19 for Metro comes out every error returned to your code is &#8216;COMException&#8217; at runtime, not being able to find out useful details while debugging would be a nightmare.)\u00a0 Almost makes one wish for the return of GetLastError&#8230;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>WinRT, so much new and interesting&#8230; so much old and broken?\u00a0 I&#8217;ve read a few articles now, watched a couple of talk recordings &#8211; nothing great and in-depth but I think I&#8217;ve picked up a few details. WinRT is effectively the combination of &#8216;COM done better&#8217; and &#8216;OS API done using the result&#8217;. On the &hellip; <a href=\"https:\/\/www.themissingdocs.net\/?p=442\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">WinRT and the return of the memory leak&#8230;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-442","post","type-post","status-publish","format-standard","hentry","category-net-stuff"],"_links":{"self":[{"href":"https:\/\/www.themissingdocs.net\/index.php?rest_route=\/wp\/v2\/posts\/442","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.themissingdocs.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.themissingdocs.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.themissingdocs.net\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.themissingdocs.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=442"}],"version-history":[{"count":0,"href":"https:\/\/www.themissingdocs.net\/index.php?rest_route=\/wp\/v2\/posts\/442\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.themissingdocs.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=442"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.themissingdocs.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=442"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.themissingdocs.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=442"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}