October 30th, 2023

What are the dire consequences of not removing all the properties that were set via SetProp?

A customer noted that the documentation for the Set­Prop function says,

Before a window is destroyed (that is, before it returns from processing the WM_NC­DESTROY message), an application must remove all entries it has added to the property list. The application must use the Remove­Prop function to remove the entries.

What are the dire consequences of failing to remove properties?

If you forgot to remove the property, the system will call Remove­Prop on your behalf, but it will also generate a debug message to remind you of your oversight.

Removing properties is good for hygiene. It prevents the system from worrying that maybe you forgot something.

Often, the value associated with the property is something that itself needs to be cleaned up, so if you fail to clean it up yourself, that’s an indication that you leaked something.

The text about cleaning up properties has been around for a very long time. I can’t prove it, but it’s possible that early versions of Windows did not clean up orphaned properties automatically and relied on the program to perform the cleanup.

Bonus chatter: The way the system automatically cleans up your forgotten properties may not be the way you wanted! Earlier versions of Windows would assume that any string properties were added by SetProp(windowHandle, "property_name", value), and it cleaned up the property by calling Globall­Deletel­Atom on the property ID. This meant that if you added the string property by atom:

ATOM property_atom = GlobalAddAtom("property_name");

SetProp(window1, MAKEINTATOM(property_atom), value);
SetProp(window2, MAKEINTATOM(property_atom), value);

and you forgot to remove the properties from the two windows, the system would double-delete the atom. The first deletion would invalidate your property_atom, and the second one might delete somebody else’s atom who got assigned the same numerical value as your now-deleted atom.

There were so many bugs traced back to people forgetting to clean up their integer atoms that Windows eventually added code to record how the property was added and to call Global­Delete­Atom only for those that were added by passing a string as the second parameter to Set­Prop.

Topics
Code

Author

Raymond has been involved in the evolution of Windows for more than 30 years. In 2003, he began a Web site known as The Old New Thing which has grown in popularity far beyond his wildest imagination, a development which still gives him the heebie-jeebies. The Web site spawned a book, coincidentally also titled The Old New Thing (Addison Wesley 2007). He occasionally appears on the Windows Dev Docs Twitter account to tell stories which convey no useful information.

5 comments

Discussion is closed. Login to edit/delete existing comments.

  • Steven Don

    Despite doing Win32 programming since the mid 90s, I don’t think I’ve ever used SetProp before. Looks like it serves a similar function to GetWindowLong(Ptr) with GWL_USERDATA (and following values with appropriate WNDCLASS::cbWndExtra), albeit more flexible yet less efficient?

    • Raymond ChenMicrosoft employee Author

      It’s primarily for the case where you want to attach data to a window that belongs to a class you didn’t write (so the GWL_USERDATA doesn’t belong to you), or to attach data in a generic way that another component can find (for example NonRudeHWND).

  • Gunnar Dalsnes

    Hmm So now some properties are no longer cleaned up? Can that cause new/different problems? Could it not be possible to have a refrence count on the properties?

    • Raymond ChenMicrosoft employee Author

      Not sure what you mean by “now some properties are no longer cleaned up.” Some very old versions of Windows may not have cleaned them up, but the cleanup has existed at least since Windows 3.1 (oldest version of Windows I have easy access to).

  • David Trapp

    Typo:

    GloballDeletelAtom

    has a tad too many L’s.