I am fresh to Visual C++. While moving forward in the book “Programming Windows with MFC”, I came across GDI (Graphics Device Interface) and use of paint brush. The book says a brush cannot be created if your GDI is low on memory.
I want to know when and how does GDI get low on memory? And more important, what is the reason that we cannot create brush when GDI is low on memory?
This depends a bit on the Version of Windows that you are targeting, but suffice to say that when you create a GDI object such as a brush or path, GDI will allocate some memory for that object either in your application memory or a shared server process memory (%SYSTEMcsrss.exe). In some versions of Windows, some of this memory is allocated in Kernel mode.
See this old, old, old article for how much memory and where
If you never release the object, GDI won’t reclaim the memory. Suffice to say, there’s a limit to how much is available (not necessarily proportional to the RAM in your PC). The actual limits vary, from Windows 95 upwards. It can be affected by overall application load (i.e. lots of graphical apps loaded, some of which are leaky), graphics driver issues, a number of concurrent users all competing for resource (e.g. Terminal server).
If you want to see it run out, do something like this:
CBitmap * pbm; while (true) { pbm = CBitmap::CreateBitmap(...); Sleep(0); }
Further reading:
Resource Leaks: Detecting, Locating, and Repairing Your Leaky GDI Code
BUG: GDI memory leak in Developer Studio IDE
GDI is low on memory – close one or more windows and try again
Out of memory messages when you have large numbers of programs running