I have checked out the MSDN documentation, and adopted the recommended pattern in this code fragment:
BitmapSymbols temp = null;
try {
using (var source = bitmaps.Symbols) {
temp = new BitmapSymbols(source, sizeSymbols);
}
_bitmapSymbols = temp;
temp = null;
} finally {
if (temp!=null) temp.Dispose();
}
Does anyone know why temp
is being reported on in this instance? I cannot see any execution paths in which temp
is not disposed and not set to `null'. Thanks in advance for any assistance.
If I move the assignment from-and-to temp
inside the using, the same warning is generated from FxCop.
The class BitmapSymbols
implements IDisposable
, and is a wrapper for a several collections of Bitmaps that ensures that they all get disposed at the same time.
Update:
The question was posed below:
Anyway, I do not see why you come up with this code rather than simply using:
_bitmapSymbols = new BitmapSymbols(source, sizeSymbols);
The reason is that not following the pattern can cause a memory leak if an exception occurs. I am writing a game that users might run for hours or days without restarting, so avoiding memory leaks is important for stability.