{"id":22483,"date":"2008-05-02T10:00:00","date_gmt":"2008-05-02T10:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2008\/05\/02\/you-didnt-know-you-could-add-properties-by-atom-and-its-a-good-thing-you-didnt-know\/"},"modified":"2008-05-02T10:00:00","modified_gmt":"2008-05-02T10:00:00","slug":"you-didnt-know-you-could-add-properties-by-atom-and-its-a-good-thing-you-didnt-know","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20080502-00\/?p=22483","title":{"rendered":"You didn&#039;t know you could add properties by atom, and it&#039;s a good thing you didn&#039;t know"},"content":{"rendered":"<p>\nAs I noted a few days ago,\n<a HREF=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2008\/04\/30\/8440201.aspx\">\nthere is weirdness associated with properties added by atom<\/a>.\nThis weirdness stems from the fact that adding properties by atom\nis really a hole in the original implementation\nrather than something designed on purpose.\n<\/p>\n<p>\nThe original 16-bit code for adding and removing properties went\nroughly like this:\n<\/p>\n<pre>\nBOOL SetProp(HWND hwnd, LPSTR pszName, HANDLE hValue)\n{\n    ... let's look only at the part that adds a new property ...\n    ATOM atm = HIWORD(pszName) ? GlobalAddAtom(pszName) : LOWORD(pszName);\n    if (atm == 0) return FALSE;\n    ... add the atom \"atm\" to the property list ...\n}\nHANDLE RemoveProp(HWND hwnd, LPSTR pszName)\n{\n    ATOM atm = HIWORD(pszName) ? GlobalFindAtom(pszName) : LOWORD(pszName);\n    if (atm == 0) return NULL;\n    ... look for the atom \"atm\" in the property list and remove it ...\n    if (!found) return NULL;\n    \/\/ clean up the atom\n    if (HIWORD(pszName)) GlobalDeleteAtom(atm);\n}\nvoid CleanPropertiesWhenWindowIsDestroyed(HWND hwnd)\n{\n    for (each property on the window) {\n        if (atm &gt;= MAXINTATOM) GlobalDeleteAtom(atm);\n    }\n    .. delete memory used for recording properties ...\n}\n<\/pre>\n<p>\nFirst, let&#8217;s look at properties set and removed via integer atoms.\nThese are simple:\nWhen setting the property, we just add it to the property list,\nand when removing the property, we remove it.\nNothing fancy going on here.\n<\/p>\n<p>\nSimilarly, there&#8217;s nothing particularly exciting going on if a property\nis set and removed by name.\nWhen setting the property, we use <code>GlobalAddAtom<\/code> to convert\nthe string to an atom (incrementing the reference count),\nand when removing it, we use <code>GlobalDeleteAtom<\/code>\nto clean it up (decrementing the reference count and removing the atom\nif the reference count goes to zero).\n<\/p>\n<p>\nFinally, when a window is destroyed with outstanding properties,\nwe clean them up by calling <code>GlobalDeleteAtom<\/code> on all\nthe string atoms, counteracting the <code>GlobalAddAtom<\/code> we\nperformed when we added the property.\n<\/p>\n<p>\nSo what&#8217;s the big deal? Looks great, right?\n<\/p>\n<p>\nSee if you can find the hole in this implementation.\n<\/p>\n<p>\nHint&nbsp;1:\nThere are actually <i>three<\/i> ways of adding and removing properties\nfrom a window,\nnot the two I led you to believe.\n&lt;!&#8211;\nYou can use an integer atom (one whose numerical value is less than\n<code>MAXINTATOM<\/code>),\nyou can use a string atom (an atom whose numerical value is between\n<code>MAXINTATOM<\/code> and <code>0xFFFF<\/code>),\nor you can use a string.\n&#8211;&gt;\n<\/p>\n<p>\nHint&nbsp;2:\nWhat happens if you mix and match these three methods?\n<\/p>\n<p>\nHint&nbsp;3:\nWhat happens to each of the three types of properties when the\nwindow manager is forced to clean them up?\n<\/p>\n<p>\nThese problems with properties were fixed a long time ago,\nbut old-timers remain wary of adding named properties by string atom.\nIt&#8217;s one of those\n<a HREF=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2007\/10\/23\/5612082.aspx\">\nsuperstitions<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>As I noted a few days ago, there is weirdness associated with properties added by atom. This weirdness stems from the fact that adding properties by atom is really a hole in the original implementation rather than something designed on purpose. The original 16-bit code for adding and removing properties went roughly like this: BOOL [&hellip;]<\/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":[2],"class_list":["post-22483","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-history"],"acf":[],"blog_post_summary":"<p>As I noted a few days ago, there is weirdness associated with properties added by atom. This weirdness stems from the fact that adding properties by atom is really a hole in the original implementation rather than something designed on purpose. The original 16-bit code for adding and removing properties went roughly like this: BOOL [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/22483","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=22483"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/22483\/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=22483"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=22483"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=22483"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}