News:

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

CloseHandle / DeleteObject with handles of zero

Started by Mark Jones, July 19, 2005, 06:07:19 PM

Previous topic - Next topic

Mark Jones

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
"To deny our impulses... foolish; to revel in them, chaos." MCJ 2003.08

tenkey

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).
A programming language is low level when its programs require attention to the irrelevant.
Alan Perlis, Epigram #8

Mark Jones

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.
"To deny our impulses... foolish; to revel in them, chaos." MCJ 2003.08

hutch--

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.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

James Ladd

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.

Mark Jones

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:
"To deny our impulses... foolish; to revel in them, chaos." MCJ 2003.08

QvasiModo

I believe CloseHandle would crash on Win95 boxes when passed a NULL handle. Could anyone confirm this?