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_
message), an application must remove all entries it has added to the property list. The application must use theNCÂDESTROY 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_
, 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
.
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?
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).
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?
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).
Typo:
has a tad too many L’s.