Many Win32 API functions have parameters specified to be “out”. For example, GetIconInfo() description says about the second parameter that The function fills in the structure’s members.
This implies that the function doesn’t ever read the original values stored in the “out” parameter – only changes them – and therefore the caller is free to skip initialization.
Yet in one project I see the following:
ICONINFO ii;
::SecureZeroMemory(&ii, sizeof(ICONINFO));
if (::GetIconInfo(hIcon, &ii))
{
//do stuff, then
//release bitmaps
if(ii.hbmMask)
::DeleteObject(ii.hbmMask);
if(ii.hbmColor)
::DeleteObject(ii.hbmColor);
}
Is there any sense in that SecureZeroMemory() call? What could happen without it?
Well, in general I think initialisation is not needed, but good practice if you don’t know exactly what the called function does with the values in the output variable.
In this specific case, the
ICONINFOstructure has twoHBITMAPmembers which are essentially pointers to bitmaps. In the general case I’d say that if you are passing pointers to a function then you have to be certain that:function you call creates the thing
pointed to for you and makes sure
your pointer points to it. (and probably leaves you to manage the newly allocated stuff) or
something (i.e. you allocated something for it) and the
function uses what you allocated.
The
GetIconInfo()function fits the first case. So for clarity and perhaps even security it looks like a good idea to me to ensure theHBITMAPmembers of theICONINFOstructure are actually zero, rather than a random value that can lead to all kinds of nastiness further down the road.So my verdict in this case would also be: not necessary but good practice.