{"id":109307,"date":"2024-01-23T07:00:00","date_gmt":"2024-01-23T15:00:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/oldnewthing\/?p=109307"},"modified":"2024-01-23T07:25:36","modified_gmt":"2024-01-23T15:25:36","slug":"20240123-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20240123-00\/?p=109307","title":{"rendered":"The useless <CODE>IMemory&shy;Buffer&shy;Reference.Closed<\/CODE> event"},"content":{"rendered":"<p>The <code>IMemory\u00adBuffer\u00adReference<\/code> interface has a <code>Closed<\/code> event which is signaled when the underlying object is closed or destructed. Closing the <code>IMemory\u00adBuffer\u00adReference<\/code> invalidates any pointers that had been obtained from <code>GetBuffer<\/code>.<\/p>\n<p>Unfortunately, this event is basically useless.<\/p>\n<p>The idea was that a consumer of the buffer could be notified that the underlying data has been made unavailable, but multithreading means that this notification doesn&#8217;t really help: Suppose that you have one thread that is doing some computation with the buffer, and another thread that notifies you that the buffer is invalid. The notification thread can&#8217;t stop the computation thread in its tracks. At best, it would have to signal the computation thread to wrap up and block the notification thread until the computation thread reports that it has stopped accessing the buffer. But the notification might be delivered on a single-threaded apartment, in which case blocking is ill-advised.<\/p>\n<p>What&#8217;s worse, some implementations of <code>IMemory\u00adBuffer\u00adReference<\/code> raise the event <i>after<\/i> the buffer becomes invalid, so this event doesn&#8217;t even give you a chance to stop your computation. It&#8217;s just telling you, &#8220;Oh, hey, so, like, you probably just corrupted memory a few millseconds ago.&#8221;<\/p>\n<p>Fortunately, you don&#8217;t really need the notification because you&#8217;re generally just notifying yourself. Each <code>IMemory\u00adBuffer\u00adReference<\/code> is a separate reference to the buffer, and if you have two components that want to access the buffer, you can just give each one a different <code>IMemory\u00adBuffer\u00adReference<\/code>. That way, one component closing the <code>IMemory\u00adBuffer\u00adReference<\/code> has no effect on the other. The only time your <code>IMemory\u00adBuffer\u00adReference<\/code> should be closed is when <i>you<\/i> close it.<\/p>\n<p>And hopefully you can arrange so that you never surprise yourself.<\/p>\n<p>Next time, we&#8217;ll look at how the Closed event is not merely useless but also dangerous.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It tells you that you have already lost.<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-109307","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>It tells you that you have already lost.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/109307","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=109307"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/109307\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=109307"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=109307"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=109307"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}