News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

About deleting GDI objects

Started by Rainstorm, March 22, 2009, 08:42:19 PM

Previous topic - Next topic

Rainstorm

Hi.

do i need to Delete an object everytime a  particular type of object is selected into a DC, or does this need to be done just once at the beginning & end, for an object of a certain type ?
In the below example the brush is changed several times, but I've saved the previous handle just on the 1st instance (handle of the default brush) , & deleted just the last used brush, at the end while restoring the default brush into the DC again

    invoke GetDC, hwnd
    mov hdc, eax

    invoke CreateHatchBrush, HS_BDIAGONAL, 65535
    mov hbr, eax
    invoke SelectObject, hdc, hbr
    mov hbr_prev, eax                   ; saved the handle to the oldbrush (default)
    . . .Do some drawing operations
    invoke CreateSolidBrush, 14786333
    mov hbr, eax
    invoke SelectObject, hdc, hbr       ; old handle not saved while selecting new brush
    . . .Do some drawing operations
    invoke CreateHatchBrush, HS_VERTICAL, 216
    invoke SelectObject, hdc, eax       ; old handle not saved while selecting new brush
    . . .Do some drawing operations

    invoke SelectObject, hdc, hbr_prev  ; restore the default brush
    invoke DeleteObject, eax            ; delete just the last used brush
    invoke ReleaseDC, hwnd, hdc


Tedd

You should delete each object you create after you've finished using it (or all at the 'end,' if that's more convenient.)
No snowflake in an avalanche feels responsible.

Rainstorm

Tedd,.. thanks a lot  :U

tedd wrote...or all at the 'end,' if that's more convenient. I assume this would mean saving the handle for each used object. - in that case.. deleting each one after usage might be easier i think. I'd just have to save 2 handles that way.
    invoke CreateSolidBrush, 216
    mov hbr, eax
    invoke SelectObject, hdc, hbr         ; 1st object
    mov hbr_prev, eax
    invoke CreateSolidBrush, 65278
    mov hbr, eax
    invoke SelectObject, hdc, eax
    invoke DeleteObject, eax
    . . . and so on
   invoke SelectObject, hdc, hbr_prev
   invoke DeleteObject, eax


ecube

I know its good programming practice to cleanup after you make a mess, and I follow that, but doesn't the OS clean it up anyway if your process were to just exit?

Rainstorm

the sdk says this
QuoteFailing to delete objects causes serious performance problems.
.. don't know if that refers to just when the process is running or later too

PBrennick

Deleting objects is not necessary while the process is running but should be a part of the WM_CLOSE or WM_DESTROY (best place) processing. There is a heirarchy, also. So if you create a DC and create objects to use in that DC. You need to delete the objects and then the DC. This is a little out of my field of expertise but it goes something like that. Some of our graphics gurus may tell you more specifically.

About the OS clean-up. Do not depend on it to work all the time. This was the pratfall of the older versions of windows and was the cause of many a crash. While things are definitely better, now; I, for one, still am not trusting of the housekeeping of the OS and I also feel it is good programming practice to clean up yourself.

Paul
The GeneSys Project is available from:
The Repository or My crappy website

Tedd

The OS should clean up, yes... but in certain strange circumstances, certain objects aren't - since you don't know what voodoo goes on behind the scenes, it's safest to be a good boy :wink

There used to be a major bug, where graphics objects were still 'owned' by the gdi (some kind of memory saver) and so if you didn't clean up your objects before exiting, they wouldn't be cleaned up at all. Run your program a few too many times and the entire system grinds to a halt and runs out of memory. It seems to be a little less sensitive these days.

You should really clean up when you've finished with your objects, rather than putting it off until final exit (unless you're going to use those objects again, but I suppose then you're not really finished with them.) The performance problems will come from similar circumstances as the afore mentioned bug - graphics objects (device contexts, particularly) take large chunks of memory. So that's at least while you're running, and afterwards if you happen to leave anything behind. (Not that anyone worries about memory these days.)
No snowflake in an avalanche feels responsible.

donkey

Quote from: PBrennick on March 23, 2009, 03:01:16 PM
Deleting objects is not necessary while the process is running but should be a part of the WM_CLOSE or WM_DESTROY (best place) processing. There is a heirarchy, also. So if you create a DC and create objects to use in that DC. You need to delete the objects and then the DC. This is a little out of my field of expertise but it goes something like that. Some of our graphics gurus may tell you more specifically.

There is an absolute limit to the number of GDI handles that can be open so deleting GDI objects is critical if they are not being used. Toolbar Paint had a problem early on where computers would freeze, crash or give cryptic messages simply because of an object not being deleted. As a rule of thumb always delete any object that you have created as soon as you are finished with it, for example a bitmap used for back-buffering could easily overwhelm your system if it is not deleted once the WM_PAINT handler is done with it.

http://msdn.microsoft.com/en-us/library/ms724291(VS.85).aspx
"Ahhh, what an awful dream. Ones and zeroes everywhere...[shudder] and I thought I saw a two." -- Bender
"It was just a dream, Bender. There's no such thing as two". -- Fry
-- Futurama

Donkey's Stable