Hi,
I am facing the memory exception in the code given below while creating the
graphis object. Since "ProcessLevelOneData" function is called very
frequently so graphics objects are being created very frequently. We are
also disposing the graphics object at the end of the function.So why are we
getting the error? Please help.
Exception
----------
Type : System.OutOfMemoryException, mscorlib, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089
Message : Out of memory.
Source : System.Drawing
Help link :
Data : System.Collections.ListDictionaryInternal
TargetSite : System.Drawing.Graphics FromHwndInternal(IntPtr)
Stack Trace : at System.Drawing.Graphics.FromHwndInternal(IntPtr hwnd)
at System.Windows.Forms.Control.CreateGraphicsInternal()
at System.Windows.Forms.Control.CreateGraphics()
at Nirvana.LiveFeed.LevelOneGrid.ProcessLevelOneData(ArrayList ar)
Code
------
private void ProcessLevelOneData(ArrayList ar)
{
try
{
string symbol;
int rowToUpdate = int.MinValue;
if (grdLevelOne != null)
{
// get the graphics object for the grid
Graphics g = this.grdLevelOne.CreateGraphics();
if (this.grdLevelOne.DisplayLayout != null &&
grdLevelOne.DisplayLayout.UIElement == null)
{
return;
}
// find the row col region intersection so
// we can clip the rendering to this area
UIElement intersection =
this.grdLevelOne.DisplayLayout.UIElement.GetDescendant(typeof(RowColRegionIntersectionUIElement));
if (intersection == null)
{
return;
}
// create an instance of the disposable drawcache class
// this is used to cache pens, brushes, etc. for the
// draw operations that will occur within this tick
// operation
using (DrawCache drawCache = new DrawCache(g, intersection,
AlphaBlendMode.Optimized, false))
{
AppearanceData appearance = new AppearanceData();
// create a single UIElementDrawParams to use for all of the cell
// rendering in this tick operation
UIElementDrawParams drawParams = new UIElementDrawParams(intersection,
drawCache, intersection.Rect, ref appearance, false, false, false);
// store a copy of the original region
// so we can restore the graphics object to
// its original state
using (Region oldRegion = g.Clip)
{
// clip such that the scrollbars, etc. are not
// part of the renderable area
g.IntersectClip(intersection.Rect);
for (int i = 0; i < ar.Count; i++)
{
rowToUpdate = int.MinValue;
Level1Data e = (Level1Data)ar[i];
symbol = e.Symbol.ToString();
// sr.Write(symbol);
if (_symbolList.ContainsKey(symbol.ToUpper().Trim()))
{
if (_symbolRowDict.ContainsKey(symbol))
{
///Update Record
rowToUpdate = int.Parse(_symbolRowDict[symbol].ToString());
}
else
{
///New Record
_symbolRowDict.Add(symbol, this.ultraDataSource1.Rows.Count);
}
GridBindingDelegate d = new GridBindingDelegate(UpdateGrid); //Instantiate a
Delegate To Bind The Grid
this.Invoke(d, new object[] { rowToUpdate, e, g, drawParams }); //Invoke The
Delegate
}
}
}
}
if (g != null)
g.Dispose();
}
_updateStarted = false;
catch (Exception ex)
{
// Invoke our policy that is responsible for making sure no secure
information
// gets out of our layer.
bool rethrow = ExceptionPolicy.HandleException(ex,
Common.POLICY_LOGANDSHOW);
if (rethrow)
{
throw;
}
}
}
Regards,
Rajat.
Kevin Spencer - 22 Mar 2007 18:06 GMT
Don't use the CreateGraphics method of a Control to get an instance of the
Graphics object. Create it independently of any Control that needs drawing.
One way is to use a Bitmap to get the Graphics from.

Signature
HTH,
Kevin Spencer
Microsoft MVP
Printing Components, Email Components,
Networking Components, Controls, much more.
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net
> Hi,
>
[quoted text clipped - 188 lines]
>
> Rajat.
Rajat - 23 Mar 2007 06:56 GMT
Hi Kevin,
Thanks for the prompt reply. Can you also tell the reason of recommending
independent graphics object using Bitmap instead of control's graphics
object.
Regards,
Rajat.
> Don't use the CreateGraphics method of a Control to get an instance of the
> Graphics object. Create it independently of any Control that needs
[quoted text clipped - 192 lines]
>>
>> Rajat.
Kevin Spencer - 23 Mar 2007 13:52 GMT
Hi Rajat,
Here's an article on the topic: http://www.bobpowell.net/creategraphics.htm

Signature
HTH,
Kevin Spencer
Microsoft MVP
Printing Components, Email Components,
Networking Components, Controls, much more.
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net
> Hi Kevin,
>
[quoted text clipped - 202 lines]
>>>
>>> Rajat.