Resource leak detection

Code review rules

You can detect possible API resource leaks with Project Analyzer by letting it require that Win32 API resource handles are released. A resource handle is a variable that stores the resource handle returned by an API function. You can require that such a handle be explicitly destroyed via a corresponding API call. The release call possibly deletes the handle, frees the underlying memory object and releases any resources associated with it.

This feature allows you to find code that could potentially be leaking resources. Because this is a mechanical check, you cannot rely on it to make sure that all resources are actually released at run-time. The feature is meant to trigger your attention. You still need to ensure your program logic manually.

Resource leak detection is part of the problem detection feature. It's a subtype of the variable clearing rules.

Rules

Handle write is an assignment statement that stores the result of an API function call returning a resource handle. The syntax supported is h = apifunc(...).

Handle variable (h) is a variable that stores the results of a handle write.

Handle release is an API function call that releases or destroys the resources associated with a handle. The syntax supported is releaseapi(h).

Handle write and release must use the same handle variable (h above). You can require any of the following:

These are the same rules as those with object variable clearning.

The currently supported API functions are the Win32 GDI functions that return a resource handle that you're required to release. The functions are listed below.

Limitations

Handle variable is not a variable that stores the return value of a VB function or the contents of another variable. An assignment to a property is not a handle write in this analysis. Thus, if you assign the handle to another variable or property or return it as a function return value, this analysis does not work.

This feature requires that you store and release the handle using a single variable. If you assign the handle to another variable, store it in a property, pass it as a parameter or in the function return value, the feature doesn't work in this case. What this means is that the following kind of code will trigger an error:

hDC = GetDC(0) ' Resource not released: hDC
hDC2 = hDC
ReleaseDC hDC2

Here is how Project Analyzer interprets this code: hDC stores the handle. hDC is not used as a parameter to ReleaseDC, so it's flagged as problematic. hDC2 is an ordinary variable because it doesn't directly store the results of the GetDC call. There is no rule for hDC2 - if you leave out ReleaseDC hDC2, you won't see a problem icon.

A ByRef parameter is not subject to the release rules because you might be passing the handle in the parameter back to the callers. A ByVal parameter is subject to the rules if you use it to store a newly created handle to it within the procedure body.

Additional use

You can use this feature to point out functions that may create and return handles. A function that creates a handle but does not seem to release it potentially returns the handle in its return value.

Supported API procedures

The supported API procedures are listed here. Functions that create and return a handle are in normal font. The corresponding release calls are displayed in italics. A variable that stores the return value of a create function is required to be passed to a corresponding release procedure.

LoadBitmapA LoadBitmapW DeleteObject

CreateBitmap CreateBitmapIndirect CreateCompatibleBitmap CreateDIBSection CreateDIBitmap DeleteObject

CreateBrushIndirect CreateSolidBrush CreatePatternBrush CreateDIBPatternBrush CreateDIBPatternBrushPt CreateHatchBrush DeleteObject

CopyCursor CreateCursor DestroyCursor

CreateCompatibleDC CreateDCA CreateDCW CreateICA CreateICW DeleteDC

GetDC GetDCEx GetWindowDC ReleaseDC

CreateFontA CreateFontW CreateFontIndirectA CreateFontIndirectW CreateFontIndirectExW DeleteObject

CreateIcon CreateIconIndirect CopyIcon DestroyIcon

DuplicateIcon ExtractIconA ExtractIconW ExtractIconExA ExtractIconExW DestroyIcon

CopyImage DeleteObject DestroyCursor DestroyIcon

CreateMetaFileA CreateMetaFileW CloseMetaFile

CloseMetaFile GetMetaFileA GetMetaFileW CopyMetaFileA CopyMetaFileW DeleteMetaFile

CreateEnhMetaFileA CreateEnhMetaFileW CloseEnhMetaFile

CloseEnhMetaFile CopyEnhMetafileA CopyEnhMetafileW GetEnhMetaFileA GetEnhMetaFileW DeleteEnhMetaFile

CreatePen CreatePenIndirect ExtCreatePen DeleteObject

PathToRegion CreateEllipticRgn CreateEllipticRgnIndirect CreatePolygonRgn CreatePolyPolygonRgn CreateRectRgn CreateRectRgnIndirect CreateRoundRectRgn ExtCreateRegion DeleteObject

CreateHalftonePalette CreatePalette DeleteObject

HeapCreate HeapDestroy

GlobalAlloc GlobalFree GlobalReAlloc GlobalDiscard

GlobalReAlloc GlobalDiscard GlobalFree GlobalReAlloc GlobalDiscard

LocalAlloc LocalFree LocalReAlloc LocalDiscard

LocalReAlloc LocalDiscard LocalFree LocalReAlloc LocalDiscard

See also

Problem detection
Code review rules

©Aivosto Oy - Project Analyzer Help Contents