I'm running PREfast static code analysis on my our projects, and it is giving me C6001 'using uninitialized memory' errors for this pattern:
// AutoSelectGDIObject is a class
if (AutoSelectGDIObject & select_image = AutoSelectGDIObject(hDCImgSource, hBmp))
{
// use hDCImgSource knowing that hBmp is selected into it
}
// now we are guaranteed that hDCImgSource has had hBmp removed and its previous bmp re-selected into itself
The trick I'm trying to exploit is to allow the scoping of select_image to just the if-expression (i.e. if (condition) { expression-block = lifetime of condition variable }.
VS has happily compiled (and presumably run this) for quite some time. I haven't single stepped code like this in a long time, but as far as I can tell, it only enters the if block if select_image's operator bool() returns true, and it destroys the instance of select_image upon exiting that if block.
Is PREfast wrong? Or is there something subtle here that makes my above code and assumptions incorrect?