473,790 Members | 2,481 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Out of Memory Exception

I have an app that's dealing with a few hundred bitmap thumbnails.

I only instantiate the bitmaps that are visible at a given time and dispose
of them if they get scrolled out of view. I've used the .NET memory profiler
and I've verified there's no leak in my bitmaps, nor are there any other
apparent leaks. So, at a given time, there may be 30 or so bitmaps displayed
at a given time.

Still, somehow, I'm getting random errors in the painting that I can only
assume is coming from some sort of GDI resource issue. I've also had
situations where painting would happen outside of the control, where GDI was
clearly confused.

Here's the tail end of a stack dump from a recent OutOfMemory exception:

system.drawing. dll!System.Draw ing.Graphics.Fr omHdcInternal(i nt hdc = 0) +
0x56 bytes
system.windows. forms.dll!Syste m.Windows.Forms .GraphicsBuffer Manager.DibGrap hicsBufferManag er.CreateBuffer (int
src = 738299658, int offsetX = 0, int offsetY = 0, int width = 1333, int
height = 1086) + 0x1e4 bytes
system.windows. forms.dll!Syste m.Windows.Forms .GraphicsBuffer Manager.DibGrap hicsBufferManag er.AllocBuffer( System.Drawing. Graphics
targetGraphics = <undefined value>, int targetDC = 738299658,
System.Drawing. Rectangle targetBounds = {X=0 Y=0 Width=1333 Height=1086}) +
0x1a4 bytes
system.windows. forms.dll!Syste m.Windows.Forms .GraphicsBuffer Manager.DibGrap hicsBufferManag er.AllocBufferI nTempManager(Sy stem.Drawing.Gr aphics
targetGraphics = <undefined value>, int targetDC = 738299658,
System.Drawing. Rectangle targetBounds = {X=0 Y=0 Width=1333 Height=1086}) +
0x3e bytes
system.windows. forms.dll!Syste m.Windows.Forms .GraphicsBuffer Manager.DibGrap hicsBufferManag er.AllocBuffer( int
target = 738299658, System.Drawing. Rectangle targetBounds = {X=0 Y=0
Width=1333 Height=1086}) + 0x3f bytes
system.windows. forms.dll!Syste m.Windows.Forms .Control.WmPain t(System.Window s.Forms.Message
m = {System.Windows .Forms.Message} ) + 0x23a bytes
system.windows. forms.dll!Syste m.Windows.Forms .Control.WndPro c(System.Window s.Forms.Message
m = {System.Windows .Forms.Message} ) + 0x2d6 bytes

The code that triggers this is actually an Application.DoE vents() call, so
it's simply the OnPaint being called, which is where the control paints the
thumbnails.

Any ideas on how I can diagnose this? I was sure .NET Memory Profiler would
show something leaking, but there's just nothing leaking that I can find..
The app is using about 70MB of ram, but it stays pretty consistently around
that figure through the lifetime of the app and is well within my 2.5GB of
physical ram.

I do make some P/Invoke GDI calls in the OnPaint. I have a method that draw
captions for each thumbnail and it has to call GetTextExtentPo int32 to
determine the text size. To do this, I basically do:

IntPtr hdc = g.GetHdc();
IntPtr oldFont = SelectObject(hd c, _labelFont.ToHf ont());
SIZE textSize = new SIZE();
GetTextExtentPo int32(hdc, nameTxt, nameTxt.Length, ref textSize);
SelectObject(hd c, oldFont);
g.ReleaseHdc(hd c);

Am I doing something wrong here that's causing the HDC to leak?

Thanks for any ideas anyone can provide in tracking this down.
Nov 18 '05 #1
11 21031
Hi Pete,
I only instantiate the bitmaps that are visible at a given >time and dispose
of them if they get scrolled out of view. Still, somehow, I'm getting random errors in the painting >that I can only
assume is coming from some sort of GDI resource >issue.


I also had to write code that generating thumbnails some time ago. .NET
throws OutOfMemoryExce ption if it does not recognize the image format.
This happen for example when loading the image file whose format is
unreconized/unsupported by .NET. Methods might raise this exception
include Image.FromFile, Bitmap constructor, etc, see their "remark"
section. You should debug your app to see if it is the cause.

Regards,
Thi

Thi

Nov 18 '05 #2
Uou're right, and I realize this. In fact, I catch the exception if it
happens on the load and report an invalid file format..

In this case, as you can see from the stack trace, the OutOfMemoryExce ption,
however, the exception is being thrown in
System.Drawing. Graphics.FromHD CInternal and is being triggered by a WM_PAINT
message, not an image load.

Pete

"Truong Hong Thi" <th*****@gmail. com> wrote in message
news:11******** *************@f 14g2000cwb.goog legroups.com...
Hi Pete,
I only instantiate the bitmaps that are visible at a given >time and
dispose
of them if they get scrolled out of view.

Still, somehow, I'm getting random errors in the painting >that I can only
assume is coming from some sort of GDI resource >issue.


I also had to write code that generating thumbnails some time ago. .NET
throws OutOfMemoryExce ption if it does not recognize the image format.
This happen for example when loading the image file whose format is
unreconized/unsupported by .NET. Methods might raise this exception
include Image.FromFile, Bitmap constructor, etc, see their "remark"
section. You should debug your app to see if it is the cause.

Regards,
Thi

Thi

Nov 18 '05 #3
Question is how large are the bitmaps?

Willy.

"Pete Davis" <pdavis68@[nospam]hotmail.com> wrote in message
news:vJ******** *************** *******@giganew s.com...
I have an app that's dealing with a few hundred bitmap thumbnails.

I only instantiate the bitmaps that are visible at a given time and
dispose of them if they get scrolled out of view. I've used the .NET
memory profiler and I've verified there's no leak in my bitmaps, nor are
there any other apparent leaks. So, at a given time, there may be 30 or so
bitmaps displayed at a given time.

Still, somehow, I'm getting random errors in the painting that I can only
assume is coming from some sort of GDI resource issue. I've also had
situations where painting would happen outside of the control, where GDI
was clearly confused.

Here's the tail end of a stack dump from a recent OutOfMemory exception:

system.drawing. dll!System.Draw ing.Graphics.Fr omHdcInternal(i nt hdc = 0) +
0x56 bytes

system.windows. forms.dll!Syste m.Windows.Forms .GraphicsBuffer Manager.DibGrap hicsBufferManag er.CreateBuffer (int
src = 738299658, int offsetX = 0, int offsetY = 0, int width = 1333, int
height = 1086) + 0x1e4 bytes

system.windows. forms.dll!Syste m.Windows.Forms .GraphicsBuffer Manager.DibGrap hicsBufferManag er.AllocBuffer( System.Drawing. Graphics
targetGraphics = <undefined value>, int targetDC = 738299658,
System.Drawing. Rectangle targetBounds = {X=0 Y=0 Width=1333 Height=1086})
+ 0x1a4 bytes

system.windows. forms.dll!Syste m.Windows.Forms .GraphicsBuffer Manager.DibGrap hicsBufferManag er.AllocBufferI nTempManager(Sy stem.Drawing.Gr aphics
targetGraphics = <undefined value>, int targetDC = 738299658,
System.Drawing. Rectangle targetBounds = {X=0 Y=0 Width=1333 Height=1086})
+ 0x3e bytes

system.windows. forms.dll!Syste m.Windows.Forms .GraphicsBuffer Manager.DibGrap hicsBufferManag er.AllocBuffer( int
target = 738299658, System.Drawing. Rectangle targetBounds = {X=0 Y=0
Width=1333 Height=1086}) + 0x3f bytes

system.windows. forms.dll!Syste m.Windows.Forms .Control.WmPain t(System.Window s.Forms.Message
m = {System.Windows .Forms.Message} ) + 0x23a bytes

system.windows. forms.dll!Syste m.Windows.Forms .Control.WndPro c(System.Window s.Forms.Message
m = {System.Windows .Forms.Message} ) + 0x2d6 bytes

The code that triggers this is actually an Application.DoE vents() call, so
it's simply the OnPaint being called, which is where the control paints
the thumbnails.

Any ideas on how I can diagnose this? I was sure .NET Memory Profiler
would show something leaking, but there's just nothing leaking that I can
find.. The app is using about 70MB of ram, but it stays pretty
consistently around that figure through the lifetime of the app and is
well within my 2.5GB of physical ram.

I do make some P/Invoke GDI calls in the OnPaint. I have a method that
draw captions for each thumbnail and it has to call GetTextExtentPo int32
to determine the text size. To do this, I basically do:

IntPtr hdc = g.GetHdc();
IntPtr oldFont = SelectObject(hd c, _labelFont.ToHf ont());
SIZE textSize = new SIZE();
GetTextExtentPo int32(hdc, nameTxt, nameTxt.Length, ref textSize);
SelectObject(hd c, oldFont);
g.ReleaseHdc(hd c);

Am I doing something wrong here that's causing the HDC to leak?

Thanks for any ideas anyone can provide in tracking this down.

Nov 18 '05 #4
"Willy Denoyette [MVP]" <wi************ *@telenet.be> wrote in message
news:eL******** ******@TK2MSFTN GP15.phx.gbl...
Question is how large are the bitmaps?

Willy.


Willy, some of the bitmaps are quite large, but they are freed before the
thumbnails are drawn and all that remains loaded are whatever thumbnails are
visible at a given time. So, for example, I may have 30 or 40 100x100
bitmaps. The problem was happening regardless of whether the bitmaps were
created from the original image, or from my thumbnail cache (which keeps a
copy of the thumbnail after the first time it's created).

After some testing, I think I've narrowed it down to the code that draws the
caption text under each thumbnail. Though the problem isn't 100% reliably
reproduceable, I've done enough testing now that I would think I would have
run into the problem a few times by now, but haven't, since I removed the
code to draw the text.

Here is the full code that draws the text:

private void DrawFileText(Gr aphics g, Point thumbLocation, ThumbInfo ti)
{
IntPtr hdc = g.GetHdc();
IntPtr oldFont = SelectObject(hd c, _labelFont.ToHf ont());

string nameTxt = Path.GetFileNam e(ti.Filename);
if (nameTxt.Length > 25)
{
nameTxt = nameTxt.Substri ng(0, 23) + "...";
}

SIZE textSize = new SIZE();
GetTextExtentPo int32(hdc, nameTxt, nameTxt.Length, ref textSize);

int xNameLoc = (thumbLocation. X + (_thumbSize.Wid th / 2)) - (textSize.cx
/ 2);
int yNameLoc = thumbLocation.Y + _thumbSize.Heig ht + 3;

string dimTxt = ti.ImageSize.Wi dth.ToString() + "x" +
ti.ImageSize.He ight.ToString() ;
GetTextExtentPo int32(hdc, dimTxt, dimTxt.Length, ref textSize);

int xDimLoc = (thumbLocation. X + (_thumbSize.Wid th / 2)) - (textSize.cx
/ 2);
int yDimLoc = yNameLoc + textSize.cy;

SelectObject(hd c, oldFont);
g.ReleaseHdc(hd c);

g.DrawString(na meTxt, _labelFont, Brushes.Black, new PointF((float)
xNameLoc, (float) yNameLoc));
g.DrawString(di mTxt, _labelFont, Brushes.Black, new PointF((float)
xDimLoc, (float) yDimLoc));
}

There must be a leak in here somewhere that .NET Memory Profiler isn't
catching. If I comment out the call to this method, I simply can't seem to
reproduce the problem. I will continue testing to try to be certain.

Pete
Nov 18 '05 #5
[snip]
Here is the full code that draws the text:

private void DrawFileText(Gr aphics g, Point thumbLocation, ThumbInfo ti)
{
IntPtr hdc = g.GetHdc();
IntPtr oldFont = SelectObject(hd c, _labelFont.ToHf ont());

string nameTxt = Path.GetFileNam e(ti.Filename);
if (nameTxt.Length > 25)
{
nameTxt = nameTxt.Substri ng(0, 23) + "...";
}

SIZE textSize = new SIZE();
GetTextExtentPo int32(hdc, nameTxt, nameTxt.Length, ref textSize);

int xNameLoc = (thumbLocation. X + (_thumbSize.Wid th / 2)) -
(textSize.cx / 2);
int yNameLoc = thumbLocation.Y + _thumbSize.Heig ht + 3;

string dimTxt = ti.ImageSize.Wi dth.ToString() + "x" +
ti.ImageSize.He ight.ToString() ;
GetTextExtentPo int32(hdc, dimTxt, dimTxt.Length, ref textSize);

int xDimLoc = (thumbLocation. X + (_thumbSize.Wid th / 2)) - (textSize.cx
/ 2);
int yDimLoc = yNameLoc + textSize.cy;

SelectObject(hd c, oldFont);
g.ReleaseHdc(hd c);

g.DrawString(na meTxt, _labelFont, Brushes.Black, new PointF((float)
xNameLoc, (float) yNameLoc));
g.DrawString(di mTxt, _labelFont, Brushes.Black, new PointF((float)
xDimLoc, (float) yDimLoc));
}


I have little doubt, at this point, that the above code is the problem. I
just can't figure out what's wrong with it. If anyone has any ideas, I'd
really appreciate it.

If the call to this method is commented out (it's called from OnPaint), then
everything works fine. If this method isn't commented out, eventually I get
an error in the paint, somewhere deep in GDI and usually not with this
particular OnPaint in the call stack. It appears that this method is somehow
corrupting GDI or perhaps leaking GDI resources or something.

The Dllmports for the two API functions used are:

[DllImport("gdi3 2.dll", CharSet = CharSet.Unicode )]
public static extern int GetTextExtentPo int32(IntPtr hdc, String str, int
len, ref SIZE size);

[DllImport("gdi3 2.dll")]
static public extern IntPtr SelectObject(In tPtr hDC, IntPtr hObject);
The SIZE struct is:

[StructLayout(La youtKind.Sequen tial)]
public struct SIZE
{
public int cx;
public int cy;
}
Can anyone spot a problem anywhere in there?

Thanks.

Pete
Nov 18 '05 #6
A while back, I had Out of Memory exceptions in one of my applications even
though the development PC had 2GB of memory (no memory leaks). I found this
during a Google search:

http://support.microsoft.com/kb/825680/

I applied the .NET Framework 1.1 Service Pack 1 and it fixed the issue.
This may or may not help you, but it is something to consider.

"Pete Davis" wrote:
I have an app that's dealing with a few hundred bitmap thumbnails.

I only instantiate the bitmaps that are visible at a given time and dispose
of them if they get scrolled out of view. I've used the .NET memory profiler
and I've verified there's no leak in my bitmaps, nor are there any other
apparent leaks. So, at a given time, there may be 30 or so bitmaps displayed
at a given time.

Still, somehow, I'm getting random errors in the painting that I can only
assume is coming from some sort of GDI resource issue. I've also had
situations where painting would happen outside of the control, where GDI was
clearly confused.

Here's the tail end of a stack dump from a recent OutOfMemory exception:

system.drawing. dll!System.Draw ing.Graphics.Fr omHdcInternal(i nt hdc = 0) +
0x56 bytes
system.windows. forms.dll!Syste m.Windows.Forms .GraphicsBuffer Manager.DibGrap hicsBufferManag er.CreateBuffer (int
src = 738299658, int offsetX = 0, int offsetY = 0, int width = 1333, int
height = 1086) + 0x1e4 bytes
system.windows. forms.dll!Syste m.Windows.Forms .GraphicsBuffer Manager.DibGrap hicsBufferManag er.AllocBuffer( System.Drawing. Graphics
targetGraphics = <undefined value>, int targetDC = 738299658,
System.Drawing. Rectangle targetBounds = {X=0 Y=0 Width=1333 Height=1086}) +
0x1a4 bytes
system.windows. forms.dll!Syste m.Windows.Forms .GraphicsBuffer Manager.DibGrap hicsBufferManag er.AllocBufferI nTempManager(Sy stem.Drawing.Gr aphics
targetGraphics = <undefined value>, int targetDC = 738299658,
System.Drawing. Rectangle targetBounds = {X=0 Y=0 Width=1333 Height=1086}) +
0x3e bytes
system.windows. forms.dll!Syste m.Windows.Forms .GraphicsBuffer Manager.DibGrap hicsBufferManag er.AllocBuffer( int
target = 738299658, System.Drawing. Rectangle targetBounds = {X=0 Y=0
Width=1333 Height=1086}) + 0x3f bytes
system.windows. forms.dll!Syste m.Windows.Forms .Control.WmPain t(System.Window s.Forms.Message
m = {System.Windows .Forms.Message} ) + 0x23a bytes
system.windows. forms.dll!Syste m.Windows.Forms .Control.WndPro c(System.Window s.Forms.Message
m = {System.Windows .Forms.Message} ) + 0x2d6 bytes

The code that triggers this is actually an Application.DoE vents() call, so
it's simply the OnPaint being called, which is where the control paints the
thumbnails.

Any ideas on how I can diagnose this? I was sure .NET Memory Profiler would
show something leaking, but there's just nothing leaking that I can find..
The app is using about 70MB of ram, but it stays pretty consistently around
that figure through the lifetime of the app and is well within my 2.5GB of
physical ram.

I do make some P/Invoke GDI calls in the OnPaint. I have a method that draw
captions for each thumbnail and it has to call GetTextExtentPo int32 to
determine the text size. To do this, I basically do:

IntPtr hdc = g.GetHdc();
IntPtr oldFont = SelectObject(hd c, _labelFont.ToHf ont());
SIZE textSize = new SIZE();
GetTextExtentPo int32(hdc, nameTxt, nameTxt.Length, ref textSize);
SelectObject(hd c, oldFont);
g.ReleaseHdc(hd c);

Am I doing something wrong here that's causing the HDC to leak?

Thanks for any ideas anyone can provide in tracking this down.

Nov 18 '05 #7
I already have the service pack installed. It would have been nice if that
were the problem :-)

Pete

"rmacias" <rm*****@newsgr oup.nospam> wrote in message
news:EB******** *************** ***********@mic rosoft.com...
A while back, I had Out of Memory exceptions in one of my applications even
though the development PC had 2GB of memory (no memory leaks). I found
this
during a Google search:

http://support.microsoft.com/kb/825680/

I applied the .NET Framework 1.1 Service Pack 1 and it fixed the issue.
This may or may not help you, but it is something to consider.

"Pete Davis" wrote:
I have an app that's dealing with a few hundred bitmap thumbnails.

I only instantiate the bitmaps that are visible at a given time and
dispose
of them if they get scrolled out of view. I've used the .NET memory
profiler
and I've verified there's no leak in my bitmaps, nor are there any other
apparent leaks. So, at a given time, there may be 30 or so bitmaps
displayed
at a given time.

Still, somehow, I'm getting random errors in the painting that I can only
assume is coming from some sort of GDI resource issue. I've also had
situations where painting would happen outside of the control, where GDI
was
clearly confused.

Here's the tail end of a stack dump from a recent OutOfMemory exception:

system.drawing. dll!System.Draw ing.Graphics.Fr omHdcInternal(i nt hdc = 0)
+
0x56 bytes

system.windows. forms.dll!Syste m.Windows.Forms .GraphicsBuffer Manager.DibGrap hicsBufferManag er.CreateBuffer (int
src = 738299658, int offsetX = 0, int offsetY = 0, int width = 1333, int
height = 1086) + 0x1e4 bytes

system.windows. forms.dll!Syste m.Windows.Forms .GraphicsBuffer Manager.DibGrap hicsBufferManag er.AllocBuffer( System.Drawing. Graphics
targetGraphics = <undefined value>, int targetDC = 738299658,
System.Drawing. Rectangle targetBounds = {X=0 Y=0 Width=1333 Height=1086})
+
0x1a4 bytes

system.windows. forms.dll!Syste m.Windows.Forms .GraphicsBuffer Manager.DibGrap hicsBufferManag er.AllocBufferI nTempManager(Sy stem.Drawing.Gr aphics
targetGraphics = <undefined value>, int targetDC = 738299658,
System.Drawing. Rectangle targetBounds = {X=0 Y=0 Width=1333 Height=1086})
+
0x3e bytes

system.windows. forms.dll!Syste m.Windows.Forms .GraphicsBuffer Manager.DibGrap hicsBufferManag er.AllocBuffer( int
target = 738299658, System.Drawing. Rectangle targetBounds = {X=0 Y=0
Width=1333 Height=1086}) + 0x3f bytes

system.windows. forms.dll!Syste m.Windows.Forms .Control.WmPain t(System.Window s.Forms.Message
m = {System.Windows .Forms.Message} ) + 0x23a bytes

system.windows. forms.dll!Syste m.Windows.Forms .Control.WndPro c(System.Window s.Forms.Message
m = {System.Windows .Forms.Message} ) + 0x2d6 bytes

The code that triggers this is actually an Application.DoE vents() call,
so
it's simply the OnPaint being called, which is where the control paints
the
thumbnails.

Any ideas on how I can diagnose this? I was sure .NET Memory Profiler
would
show something leaking, but there's just nothing leaking that I can
find..
The app is using about 70MB of ram, but it stays pretty consistently
around
that figure through the lifetime of the app and is well within my 2.5GB
of
physical ram.

I do make some P/Invoke GDI calls in the OnPaint. I have a method that
draw
captions for each thumbnail and it has to call GetTextExtentPo int32 to
determine the text size. To do this, I basically do:

IntPtr hdc = g.GetHdc();
IntPtr oldFont = SelectObject(hd c, _labelFont.ToHf ont());
SIZE textSize = new SIZE();
GetTextExtentPo int32(hdc, nameTxt, nameTxt.Length, ref textSize);
SelectObject(hd c, oldFont);
g.ReleaseHdc(hd c);

Am I doing something wrong here that's causing the HDC to leak?

Thanks for any ideas anyone can provide in tracking this down.

Nov 18 '05 #8

"Pete Davis" <pdavis68@[nospam]hotmail.com> wrote in message
news:wq******** *************** *******@giganew s.com...
[snip]
Here is the full code that draws the text:

private void DrawFileText(Gr aphics g, Point thumbLocation, ThumbInfo ti)
{
IntPtr hdc = g.GetHdc();
IntPtr oldFont = SelectObject(hd c, _labelFont.ToHf ont());

string nameTxt = Path.GetFileNam e(ti.Filename);
if (nameTxt.Length > 25)
{
nameTxt = nameTxt.Substri ng(0, 23) + "...";
}

SIZE textSize = new SIZE();
GetTextExtentPo int32(hdc, nameTxt, nameTxt.Length, ref textSize);

int xNameLoc = (thumbLocation. X + (_thumbSize.Wid th / 2)) -
(textSize.cx / 2);
int yNameLoc = thumbLocation.Y + _thumbSize.Heig ht + 3;

string dimTxt = ti.ImageSize.Wi dth.ToString() + "x" +
ti.ImageSize.He ight.ToString() ;
GetTextExtentPo int32(hdc, dimTxt, dimTxt.Length, ref textSize);

int xDimLoc = (thumbLocation. X + (_thumbSize.Wid th / 2)) -
(textSize.cx / 2);
int yDimLoc = yNameLoc + textSize.cy;

SelectObject(hd c, oldFont);
g.ReleaseHdc(hd c);

g.DrawString(na meTxt, _labelFont, Brushes.Black, new PointF((float)
xNameLoc, (float) yNameLoc));
g.DrawString(di mTxt, _labelFont, Brushes.Black, new PointF((float)
xDimLoc, (float) yDimLoc));
}


I have little doubt, at this point, that the above code is the problem. I
just can't figure out what's wrong with it. If anyone has any ideas, I'd
really appreciate it.

If the call to this method is commented out (it's called from OnPaint),
then everything works fine. If this method isn't commented out, eventually
I get an error in the paint, somewhere deep in GDI and usually not with
this particular OnPaint in the call stack. It appears that this method is
somehow corrupting GDI or perhaps leaking GDI resources or something.

The Dllmports for the two API functions used are:

[DllImport("gdi3 2.dll", CharSet = CharSet.Unicode )]
public static extern int GetTextExtentPo int32(IntPtr hdc, String str, int
len, ref SIZE size);

[DllImport("gdi3 2.dll")]
static public extern IntPtr SelectObject(In tPtr hDC, IntPtr hObject);
The SIZE struct is:

[StructLayout(La youtKind.Sequen tial)]
public struct SIZE
{
public int cx;
public int cy;
}
Can anyone spot a problem anywhere in there?

Thanks.

Pete

My best guess is that somehow you are exhausting the GDI heap and handles
because you aren't deleting the Font object handle.

[DllImport("Gdi3 2.dll")]

[return: MarshalAs(Unman agedType.Bool)]

public static extern bool DeleteObject(

IntPtr hObject // handle to graphic object

);

DeleteObject(ol dFont);

Willy.


Nov 18 '05 #9
Willy,

I'll test that, but I'm curious:

The font is a member that's instantiated in the constructor and disposed in
the Dispose() method of the thumbnail control. Is ToHfont() creating a new
font handle every time I call it? The documentation doesn't say anything
about deleting the handle after calling ToHfont(). I just assumed each call
would be returning the same handle since it's the same font object.

I suppose I could just get it once in the constructor and that would take
care of the problem, if that is the problem. I'll post again after testing.
Thanks.

Pete

"Willy Denoyette [MVP]" <wi************ *@telenet.be> wrote in message
news:O$******** ******@TK2MSFTN GP12.phx.gbl...

"Pete Davis" <pdavis68@[nospam]hotmail.com> wrote in message
news:wq******** *************** *******@giganew s.com...
[snip]
Here is the full code that draws the text:

private void DrawFileText(Gr aphics g, Point thumbLocation, ThumbInfo ti)
{
IntPtr hdc = g.GetHdc();
IntPtr oldFont = SelectObject(hd c, _labelFont.ToHf ont());

string nameTxt = Path.GetFileNam e(ti.Filename);
if (nameTxt.Length > 25)
{
nameTxt = nameTxt.Substri ng(0, 23) + "...";
}

SIZE textSize = new SIZE();
GetTextExtentPo int32(hdc, nameTxt, nameTxt.Length, ref textSize);

int xNameLoc = (thumbLocation. X + (_thumbSize.Wid th / 2)) -
(textSize.cx / 2);
int yNameLoc = thumbLocation.Y + _thumbSize.Heig ht + 3;

string dimTxt = ti.ImageSize.Wi dth.ToString() + "x" +
ti.ImageSize.He ight.ToString() ;
GetTextExtentPo int32(hdc, dimTxt, dimTxt.Length, ref textSize);

int xDimLoc = (thumbLocation. X + (_thumbSize.Wid th / 2)) -
(textSize.cx / 2);
int yDimLoc = yNameLoc + textSize.cy;

SelectObject(hd c, oldFont);
g.ReleaseHdc(hd c);

g.DrawString(na meTxt, _labelFont, Brushes.Black, new PointF((float)
xNameLoc, (float) yNameLoc));
g.DrawString(di mTxt, _labelFont, Brushes.Black, new PointF((float)
xDimLoc, (float) yDimLoc));
}


I have little doubt, at this point, that the above code is the problem. I
just can't figure out what's wrong with it. If anyone has any ideas, I'd
really appreciate it.

If the call to this method is commented out (it's called from OnPaint),
then everything works fine. If this method isn't commented out,
eventually I get an error in the paint, somewhere deep in GDI and usually
not with this particular OnPaint in the call stack. It appears that this
method is somehow corrupting GDI or perhaps leaking GDI resources or
something.

The Dllmports for the two API functions used are:

[DllImport("gdi3 2.dll", CharSet = CharSet.Unicode )]
public static extern int GetTextExtentPo int32(IntPtr hdc, String str, int
len, ref SIZE size);

[DllImport("gdi3 2.dll")]
static public extern IntPtr SelectObject(In tPtr hDC, IntPtr hObject);
The SIZE struct is:

[StructLayout(La youtKind.Sequen tial)]
public struct SIZE
{
public int cx;
public int cy;
}
Can anyone spot a problem anywhere in there?

Thanks.

Pete

My best guess is that somehow you are exhausting the GDI heap and handles
because you aren't deleting the Font object handle.

[DllImport("Gdi3 2.dll")]

[return: MarshalAs(Unman agedType.Bool)]

public static extern bool DeleteObject(

IntPtr hObject // handle to graphic object

);

DeleteObject(ol dFont);

Willy.


Nov 18 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
3672
by: Alan Gifford | last post by:
I wrote a program to make sure that new would throw a bad_alloc exception if more memory was requested than was available. On my system, new allocates up to 2931 MBs of memory (I don't have that much, not even with swap) before throwing an exception. When the program tries to fill in the allocated memory with data, it is killed after using up the available memory. Does anyone know why the exception is not being thrown until well after...
21
7288
by: Stephan | last post by:
why does the following code not work???? after compiling and running it will just say killed after all my memory filled up any suggestions? #include <iostream> using namespace std; void out_of_mem() {
6
3708
by: Ganesan selvaraj | last post by:
I using C# .net. i want to split the text files based of the some condition. my source text file size may be 4 kb to 4 gb. some time when i split the i got the "out of memory exception. when i read from the file i read the 10mb and split the this content and clear the buffer and read the second 10 mb and so on. How can i rectify the problem?
6
2702
by: Fred Zwarts | last post by:
Hello, I am trying to debug some complex debug code. In order to track the use of dynamically allocated memory, I replaced the standard global new and delete operators. (Not for changing the memory allocation algorithm, but for gathering some statistics and to find memory leaks.) This seems to work. However, I noticed that my replacing delete operator is not called
2
1865
by: Aravind | last post by:
Hi, I am ruuning to out of memory exception , My system has RAM of 4 GB and virtual memory paging size set to 4GB .When I check the memory occupied by the process during the exception , it is less ( around 500 MB and peak Memory usage 800MB , even the total memory of the system is not high . what are passing use cases where the system leads to out of memory exception ? I am using two process for the program and the out of memory occuring...
1
1717
by: bw | last post by:
I have a basic custom collection that throws a new exception if the item(key) is not found in the collection. This is used as part of a calling function. It all works correctly, the problem (discovered using a memory profiler) is that the base exception being thrown in the collection is not being disposed of. I understand about the GC etc etc. It appears that something is hanging on to a reference to System.Exception and a...
2
21978
by: Ilkka | last post by:
I have created an C++ application with Windows Forms, ADO and SQL server 2005. Now I need to change something and started debugging the code. Then suddenly I receive an error. "An unhandled exception of type 'System.AccessViolationException' occurred in mscorlib.dll Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt." The progran ends on a windows form designer...
2
4891
by: =?Utf-8?B?Tm9tYW4gQWxp?= | last post by:
Hi, We are facing a strange problem in our ASP. NET website. Some times it gives the following unhandled exception. Error Message: Exception of type System.Web.HttpUnhandledException was thrown. Detailed Error Message: System.InvalidOperationException: There has been an overflow or underflow in GC memory pressure. The possible cause is unbalanced AddMemoryPressure and RemoveMemoryPressure calls. at...
1
3110
by: George2 | last post by:
Hello everyone, Such code segment is used to check whether function call or exception- handling mechanism runs out of memory first (written by Bjarne), void perverted() { try{
2
2451
by: Lambda | last post by:
I'd like to load a lot of data into a hashtable, when the memory is used up, I'll write the data to a file. I'm trying to use std::tr1::unordered_map to implement that. My question is if I insert too many data into the map, what will happen? Does unordered_map need continuous memory like vector? How to detect that there is no free memory available? Can I limit the amount of memory that is used by a container? And last, is it the right...
0
10413
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10145
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9021
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7530
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6769
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5422
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5551
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4094
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2909
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.