in Izc tutorials it was accented on that GDI objects have to be restored always. Why? Won't this slow down things? In several programs of mine I do never restore any object, never delete, and performance is the best I can imagine.
Posted on 2003-01-22 00:47:58 by Ultrano
What about memory leaks ?
Posted on 2003-01-22 00:59:53 by JCP

The GDI functions are actually wrappers for more complex memory allocation code as well as straight graphics manipulation so it is important that you always properly restore things like device contexts, delete brushes when you have finished with them and handle resources in a tidy documented way otherwise as Readiosys has mentioned, you will get memory leaks.

This effectively means you are wasting memory that is not recovered in the life of your program so if an application is a big or complex one, you could run out of memory if you did not perform the correct clean up after.

Posted on 2003-01-22 03:37:44 by hutch--
If you don't restore handles to default GDI objects, then those objects will never get released. Like Readiosys said, these are called memory leaks. When people like me use your app and find it is full of leaks, we flame you, because leaks are nearly always caused by lazy coding.

Posted on 2003-01-22 03:39:50 by sluggy
hmm. I am using System Monitor, and it shows that when I stop my program, the memory goes back to where it was before I started it- exactly the same byte count. Isn't this the way to detect memory leaks?
I use memory DCs and bitmaps, created with CreateBitmap. I do never use SelectObject on a window DC. I do not use GetDC, but GetDCEx with DCX_CACHE (similar). I do not process in wm_paint. When I select object into a memory DC, isn't it just replacing a pointer to an object with another object? The DC is just a structure, containing only pointers to objects. I do use these objects all the time, and when the proggie quits, aren't they all deleted- when the user32.dll is told that my proggie is exiting, it surely gets the arrays of objects my proggie has created emptied carefully.
I am using the memory dcs from start to the end of the program- none of them gets useless at any time while running the proggie.
I have made a test- created several thousand solid brushes, checked their availability, then exit program without deleting them. All memory is ok, windows runs perfectly.
Posted on 2003-01-22 08:21:57 by Ultrano
Windows will clean up all used resources, allocated memory, etc when your process is closed (well, win9x might not clean up everything ;) ) but as long as your program is running without cleaning up, memory will not be freed. Just ignoring this is bad programming style and your program will use much more memory than necessary. Also, 1000 brushes might not differ that much, but 1000 bitmaps (which you might get if you use some CreateBitmap function in your WM_PAINT) uses *a lot* more memory.
Just clean them up as soon as you don't need them anymore, your program speed won't change noticably since the creation functions are just as 'slow'.

Posted on 2003-01-22 10:08:30 by Thomas