Our software renders technical drawings. One particular drawing made the application crash every time with an OutOfMemoryException. Upon investigation, nothing seemed out of the ordinary; the app didn't request a lot of memory, didn't use a lot of handles. I tried catching the exception and the app finished the drawing without throwing another one. In fact, there was always only the one OutOfMemoryException, and it was always the same graphics primitive that caused it.
The following code is the minimum required to cause this particular crash. It seems that the exact combination of image size, pen style and coordinates causes the exception. Rounding the coordinates down to three decimals makes it disappear, as does making the graphics dimensions smaller or using a pen without dashing.
using (Bitmap b = new Bitmap(200, 200))
{
using (Graphics g = Graphics.FromImage(b))
{
using (Pen p = new Pen(Color.Black))
{
p.DashPattern = new float[]{10.0f, 2.0f};
RectangleF r = new RectangleF(
BitConverter.ToSingle(new byte[]{0xD3, 0x56, 0xB3, 0x42}, 0),
BitConverter.ToSingle(new byte[]{0x87, 0x2D, 0x17, 0x43}, 0),
BitConverter.ToSingle(new byte[]{0xE2, 0x81, 0xD1, 0x3F}, 0),
BitConverter.ToSingle(new byte[]{0xE2, 0x81, 0xD1, 0x3F}, 0));
float st = BitConverter.ToSingle(new byte[]{0x6B, 0xF6, 0x1A, 0x42}, 0);
float sw = BitConverter.ToSingle(new byte[]{0x6D, 0x33, 0x4F, 0x40}, 0);
g.DrawArc(p, r, st, sw);
}
}
}
In this case it's not complicated to create a workaround, but I was wondering if someone had an explanation for this.