I wonder if there is a "trick" that allows you to find out if the (used) used objects were correctly used in part of the code, or, in other words, does not create a memory leak.
Say I have a container for GDI objects (or another that I need to explicitly delete)
public class SuperPen { Pen _flatPen, _2DPen, _3DPen; public SuperPen() { _flatPen = (Pen)Pens.Black.Clone(); _2DPen = (Pen)Pens.Black.Clone(); _3DPen = (Pen)Pens.Black.Clone(); } }
Now that I need to delete the GDI objects, I:
public class SuperPen : IDisposable { Pen _flatPen, _2DPen, _3DPen; public SuperPen() { _flatPen = (Pen)Pens.Black.Clone(); _2DPen = (Pen)Pens.Black.Clone(); _3DPen = (Pen)Pens.Black.Clone(); } public void Dispose() { if (_flatPen != null) { _flatPen.Dispose(); _flatPen = null; }
A situation like this can happen if you add a new βdisposableβ object and forget to dispose of it, etc. How can I detect my mistake, I mean, check if my SuperPen was configured correctly?
source share