title: Proper way close WinAPI HANDLEs (avoiding of repeated closing)
body: <p>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?</p>
  
  <pre><code>HANDLE h;
  ....
  if ( h != INVALID_HANDLE_VALUE ) {
    ::CloseHandle(h);
    h = INVALID_HANDLE_VALUE;
  }
  </code></pre>
  
  <p>There is a same question about bitmap handles:</p>
  
  <pre><code>HBITMAP hb;
  ....
  if ( hb != INVALID_HANDLE_VALUE ) {
    ::DeleteObject(hb);
    hb = INVALID_HANDLE_VALUE;
  }
  </code></pre>
  
  <p><em><strong>EDIT:</em></strong> I think, there is some misunderstanding. I know <code>CloseHandle</code> is for closing handles. I'd like to know proper way for closing handles. A similar situations occurs with deleting of pointers. </p>
  
  <pre><code>Foo *foo = new Foo();
  
  // for example there is 2 functions that can delete foo
  void bar() {
    ....
    delete foo;
  }
  void duck() {
    ....
    delete foo;
  }
  </code></pre>
  
  <p>So, the following code means problems:</p>
  
  <pre><code>bar();
  duck();
  </code></pre>
  
  <p>There is some workaround for this case. We need to define <code>bar</code>&amp;<code>duck</code> functions like this:</p>
  
  <pre><code>void bar() {
    ....
    if (foo) {
      delete foo;
      foo = NULL;
    }
  }
  void duck() {
    ....
    if (foo) {
      delete foo;
      foo = NULL;
    }
  }
  </code></pre>
  
  <p>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?</p>