The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: Mark Jones on July 19, 2005, 06:07:19 PM

Title: CloseHandle / DeleteObject with handles of zero
Post by: Mark Jones on July 19, 2005, 06:07:19 PM
Is it okay to try deleting objects that were never created? Win32Hlp says CloseHandle returns an exception to NULL arguments, but it seems to work fine anyways. Should I really redesign this code? Because if the file does not exist, CloseHandle returns with INVALID_HANDLE or similar.


    invoke CreateFile,addr iniBuffer,GENERIC_READ,FILE_SHARE_READ,\
                      0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0
    mov hIni,eax
    .if eax!=INVALID_HANDLE_VALUE           ; if exists, put contents into edit2
        invoke ReadFile,hIni,addr szBuffer,4096,addr hIniSize,0
        invoke SetDlgItemText,hWnd,IDC_EDT2,addr szBuffer
    .endif
    invoke CloseHandle,hIni
Title: Re: CloseHandle / DeleteObject with handles of zero
Post by: tenkey on July 19, 2005, 06:50:46 PM
The current Net version of MSDN mentions exceptions only in the context of using debuggers.

See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sysinfo/base/closehandle.asp.

It might be noted that 0 is a valid file handle, which is why INVALID_HANDLE_VALUE is (-1).
Title: Re: CloseHandle / DeleteObject with handles of zero
Post by: Mark Jones on July 20, 2005, 03:52:37 AM
In the options of MemProof, it has a choice for "error on free null resource" and I just wonder if I should design my code to not generate any of these errors, or just ignore them. :) Sounds like I can ignore them. Good thing, the alternative is messy.
Title: Re: CloseHandle / DeleteObject with handles of zero
Post by: hutch-- on July 20, 2005, 04:02:56 AM
Mark,

The risk you run is that some dork at Microsoft will change something from the normal operation of a function based on their reading of the published specfication of a function and where it worked fine on one OS version, it will fall over on a later one. Its a perennial problem with Windows API functions and the only safe way is to observe the laws of gravity which means if you allocate something, clean it up after.

It usually no big deal to do if you start it the right way, when you allocate something, also write the deallocation of it at the same time, make sure it works then write the code in the guts of it that you require. It will save you grief in the longer haul.
Title: Re: CloseHandle / DeleteObject with handles of zero
Post by: James Ladd on July 21, 2005, 06:34:22 AM
Yep -

QuoteIt usually no big deal to do if you start it the right way, when you allocate something, also write the deallocation of it at the same time, make sure it works then write the code in the guts of it that you require. It will save you grief in the longer haul.

A rule to live by.
Title: Re: CloseHandle / DeleteObject with handles of zero
Post by: Mark Jones on July 21, 2005, 02:28:47 PM
Good idea. This is a little extra code, but MemProof no longer complains. :)


    .elseif eax==WM_CLOSE
        .if [active]==1                         ; if still running
            invoke MessageBox,hWnd,addr szEnd,0,MB_YESNO
            .if eax==IDYES
                jmp @F
            .else
                jmp cancel
            .endif
        .endif
@@:
        call UpdateINI                          ; create/update INI file
        .if hOFN                                ; close any open files
            invoke CloseHandle,hOFN
        .endif
        .if hSFN
           invoke CloseHandle,hSFN
        .endif
        invoke DeleteObject,hFont               ; and fonts
        invoke EndDialog,hWnd,0
cancel:
Title: Re: CloseHandle / DeleteObject with handles of zero
Post by: QvasiModo on July 21, 2005, 04:21:08 PM
I believe CloseHandle would crash on Win95 boxes when passed a NULL handle. Could anyone confirm this?