473,804 Members | 3,067 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
11 21032
This was in fact the problem and this seems to me to be a documentation
error as there's no mention of it in the documentation.

However, underneath, according to Reflector, CreateFontIndir ect is being
called each time you call ToHfont(), and obviously, the font is never
deleted, so you're obligated to do a p/Invoke to delete it if you call
ToHfont().

Pete

Nov 18 '05 #11

"Pete Davis" <pdavis68@[nospam]hotmail.com> wrote in message
news:w_******** *************** *******@giganew s.com...
This was in fact the problem and this seems to me to be a documentation
error as there's no mention of it in the documentation.

However, underneath, according to Reflector, CreateFontIndir ect is being
called each time you call ToHfont(), and obviously, the font is never
deleted, so you're obligated to do a p/Invoke to delete it if you call
ToHfont().

Pete


The call to ToHfont returns a new Windows GDI handle wrapped in an IntPtr,
so you need to call DeleteObject when done with it.
But, you are right, this isn't documented in v1.1, however, it looks like
this is corrected in the new docs's.
http://msdn2.microsoft.com/en-us/lib...t.tohfont.aspx
Willy.


Nov 18 '05 #12

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

Similar topics

4
3673
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
3709
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
1866
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
4892
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
3112
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
2452
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
10558
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...
0
10318
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10302
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,...
1
7608
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
6844
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
5503
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...
1
4277
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
2
3802
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2975
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.