{"id":106817,"date":"2022-07-01T07:00:00","date_gmt":"2022-07-01T14:00:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/oldnewthing\/?p=106817"},"modified":"2022-07-01T06:21:00","modified_gmt":"2022-07-01T13:21:00","slug":"20220701-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20220701-00\/?p=106817\/","title":{"rendered":"Under what conditions can I modify the memory that I received in the form a <CODE>STGMEDIUM<\/CODE>?"},"content":{"rendered":"<p>A customer was looking to optimize their use of data that they received from a data object in the form of a <code>STGMEDIUM<\/code>. Right now, they are making a copy of the <code>hGlobal<\/code> in the <code>STGMEDIUM<\/code> and modifying the copy. But that memory block could be quite large. Is it possible for them to just modify the original <code>hGlobal<\/code>? What are the ownership rules for the contents of a <code>STGMEDIUM<\/code>?<\/p>\n<p>The rule is that you call <code>ReleaseStgMedium<\/code> when you are finished with a <code>STGMEDIUM<\/code>. If you look at the details of the <code>ReleaseStgMedium<\/code> function, it behaves in one of two modes, depending on whether the <code>punkForRelease<\/code> member is null.<\/p>\n<table class=\"cp3\" style=\"border-collapse: collapse;\" border=\"1\" cellspacing=\"0\" cellpadding=\"3\">\n<tbody>\n<tr>\n<th rowspan=\"2\">Medium<\/th>\n<th colspan=\"3\"><code>punkForRelease<\/code><\/th>\n<\/tr>\n<tr>\n<th><code>nullptr<\/code><\/th>\n<th colspan=\"2\">Not <code>nullptr<\/code> (perform both columns)<\/th>\n<\/tr>\n<tr>\n<td><code>TYMED_HGLOBAL<\/code><\/td>\n<td><code>GlobalFree<\/code><\/td>\n<td>Nothing<\/td>\n<td style=\"font-size: 80%;\"><code>punkForRelease-&gt;Release()<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>TYMED_GDI<\/code><\/td>\n<td><code>DeleteObject<\/code><\/td>\n<td>Nothing<\/td>\n<td style=\"font-size: 80%;\"><code>punkForRelease-&gt;Release()<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>TYMED_ENHMF<\/code><\/td>\n<td><code>DeleteEnhMetaFile<\/code><\/td>\n<td>Nothing<\/td>\n<td style=\"font-size: 80%;\"><code>punkForRelease-&gt;Release()<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>TYMED_MFPICT<\/code><\/td>\n<td valign=\"bottom\"><code>DeleteMetaFile<\/code> +<br \/>\n<code>GlobalFree<\/code><\/td>\n<td valign=\"bottom\">Nothing<\/td>\n<td style=\"font-size: 80%;\" valign=\"bottom\"><code>punkForRelease-&gt;Release()<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>TYMED_FILE<\/code><\/td>\n<td valign=\"bottom\"><code>DeleteFile<\/code> +<br \/>\n<code>CoTaskMemFree<\/code><\/td>\n<td valign=\"bottom\"><code>CoTaskMemFree<\/code><\/td>\n<td style=\"font-size: 80%;\" valign=\"bottom\"><code>punkForRelease-&gt;Release()<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>TYMED_ISTREAM<\/code><\/td>\n<td><code>IStream::Release<\/code><\/td>\n<td><code>IStream::Release<\/code><\/td>\n<td style=\"font-size: 80%;\"><code>punkForRelease-&gt;Release()<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>TYMED_ISTORAGE<\/code><\/td>\n<td><code>IStorage::Release<\/code><\/td>\n<td><code>IStorage::Release<\/code><\/td>\n<td style=\"font-size: 80%;\"><code>punkForRelease-&gt;Release()<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>You can see that the model is that a null <code>punkForRelease<\/code> means that the medium is owned by the code that possesses the <code>STGMEDIUM<\/code>, whereas a non-null <code>punkForRelease<\/code> means that the medium is controlled by the <code>punkForRelease<\/code>. (In the <code>TYMED_FILE<\/code> case, the logical medium is the file on disk; the file name is always freed. And the distinction is irrelevant for <code>IStream<\/code> and <code>IStorage<\/code> cases, since the interface pointer is being released either way.)<\/p>\n<p>This means that if the <code>punkForRelease<\/code> is null, you can just treat the medium as if you owned it. In the null <code>punkForRelease<\/code> case, all <code>ReleaseStgMedium<\/code> is going to do is free the <code>hGlobal<\/code>. You can rescue that memory just before it reaches the incinerator and use it for whatever purpose you like. It was going to be destroyed anyway.<\/p>\n<p>On the other hand, if the <code>punkForRelease<\/code> is non-null, then you need to copy the memory and modify your copy, because the <code>hGlobal<\/code> is owned by the <code>punkForRelease<\/code>.\u00b9<\/p>\n<p>\u00b9 The non-null <code>punkForRelease<\/code> case typically occurs when the data object that provided the <code>STGMEDIUM<\/code> wants to cache the data across multiple calls to <code>GetData<\/code>. It creates the data once and returns the handle to each caller, but setting the cache as the <code>punkForRelease<\/code>. (In most cases, the data object acts as its own cache, so it passes itself as the <code>punkForRelease<\/code>.)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you own it, then it&#8217;s yours to do with as you wish.<\/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-106817","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>If you own it, then it&#8217;s yours to do with as you wish.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/106817","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=106817"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/106817\/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=106817"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=106817"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=106817"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}