Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8733459
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:39:18+00:00 2026-06-13T09:39:18+00:00

I have some handle and I need to close it. There is some places

  • 0

I have some handle and I need to close it. There is some places in code, where handle may be closed. So, is this a right way to close handle?

HANDLE h;
....
if ( h != INVALID_HANDLE_VALUE ) {
  ::CloseHandle(h);
  h = INVALID_HANDLE_VALUE;
}

There is a same question about bitmap handles:

HBITMAP hb;
....
if ( hb != INVALID_HANDLE_VALUE ) {
  ::DeleteObject(hb);
  hb = INVALID_HANDLE_VALUE;
}

EDIT: I think, there is some misunderstanding. I know CloseHandle is for closing handles. I’d like to know proper way for closing handles. A similar situations occurs with deleting of pointers.

Foo *foo = new Foo();

// for example there is 2 functions that can delete foo
void bar() {
  ....
  delete foo;
}
void duck() {
  ....
  delete foo;
}

So, the following code means problems:

bar();
duck();

There is some workaround for this case. We need to define bar&duck functions like this:

void bar() {
  ....
  if (foo) {
    delete foo;
    foo = NULL;
  }
}
void duck() {
  ....
  if (foo) {
    delete foo;
    foo = NULL;
  }
}

So we avoid repeated deleting of foo. The question is What is the proper way to close handles? I mean, How to avoid repeated closing handles problem?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T09:39:19+00:00Added an answer on June 13, 2026 at 9:39 am

    Not all functions that use HANDLE use CloseHandle(), some use other closing functions instead. Also, not all HANDLE values use INVALID_HANDLE_VALUE, either. Some use NULL instead.

    HBITMAP never uses INVALID_HANDLE_VALUE, it always uses NULL. And you should never call DeleteObject() for an HBITMAP you do not own.

    So the short answer is – if you are trying to create some general purpose handle management, don’t bother. You are likely to get it wrong. If you allocate/open some handle, you have to know the correct way to close it, you can’t guess at it.

    If you want the handles to manage themselves, then RAII is the best choice. I prefer to use a templated class with specialized traits to reduce code duplication for differrent types of handles, eg:

    template< class traits >
    class HandleWrapper
    {
    private:
        traits::HandleType FHandle;
    
    public:
        HandleWrapper()
            FHandle(traits::InvalidValue)
        {
        }
    
        HandleWrapper(const traits::HandleType value)
            FHandle(value)
        {
        }
    
        ~HandleWrapper()
        {
            Close();
        }
    
        void Close()
        {
            if (FHandle != traits::InvalidValue)
            {
                traits::Close(FHandle);
                FHandle = traits::InvalidValue;
            }
        }
    
        bool operator !() const {
            return (FHandle == traits:::InvalidValue);
        }
    
        operator bool() const {
            return (FHandle != traits:::InvalidValue);
        }
    
        operator traits::HandleType() {
            return FHandle;
        }
    };
    

    .

    struct KernelHandleTraits
    {
        typedef HANDLE HandleType;
        static const HANDLE InvalidValue = INVALID_HANDLE_VALUE;
    
        static void Close(HANDLE value)
        {
            CloseHandle(value);
        }
    };
    
    HandleWrapper<KernelHandleTraits> hFile(CreateFile(...));
    

    .

    struct NullKernelHandleTraits
    {
        typedef HANDLE HandleType;
        static const HANDLE InvalidValue = NULL;
    
        static void Close(HANDLE value)
        {
            CloseHandle(value);
        }
    };
    
    HandleWrapper<NullKernelHandleTraits> hMapping(CreateFileMapping(...));
    

    .

    struct FileMapViewTraits
    {    
        typedef void* HandleType;
        static const void* InvalidValue = NULL;
    
        static void Close(void *value)
        {
            UnmapViewOfFile(value);
        }
    };
    
    HandleWrapper<FileMapViewTraits> hView(MapViewOfFile(...));
    

    .

    struct GDIBitmapHandleTraits
    {    
        typedef HBITMAP HandleType;
        static const HBITMAP InvalidValue = NULL;
    
        static void Close(HBITMAP value)
        {
            DeleteObject(value);
        }
    };
    
    HandleWrapper<GDIBitmapTraits> hBmp(CreateBitmap(...));
    

    Etc.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some code in my error handler I need to test against a
I have a class that implements properties in a specific way, to handle some
I have to handle some sensitive data in my application, such as passwords, credit
Some one please tell me how to handle RunTimeExceptions in grails version1.1 .I have
I have some code which handles data files and reports an error when it
I have some data loaded as a np.ndarray and need to convert it to
I need to be able to handle/catch Intents while my Activity is closed. So
We have some client code which is using the SqlConnection class in .NET to
I have this open-source library that I'm having some trouble fixing a problem... This
I have csv file having some address data mostly in Finnish language. I need

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.