title: What is necessary cleanup for CInternetSession
body: <p>The MSDN documentation for <code>CInternetSession::Close()</code> only says</p>
  
  <blockquote>
    <p>Call this member function when your application has finished using the <code>CInternetSession</code> object.</p>
    
    <p><a href="https://msdn.microsoft.com/en-us/library/2tya7fy2.aspx" rel="noreferrer">MSDN for CInternetSession::Close</a></p>
  </blockquote>
  
  <p>For the <code>CHttpConnection</code> object returned by <code>CInternetSession::GetHttpConnection()</code>, and the <code>CHttpFile</code> object returned by <code>CHttpConnection::OpenRequest()</code>, do these objects have to be manually closed and deleted?</p>
  
  <p>I cannot find the documentation for <code>CHttpConnection::Close()</code> on MSDN, and <code>CHttpFile</code> inherits its <code>Close()</code> method from <code>CInternetFile</code>; the documentation for which is similarly unhelpful:</p>
  
  <blockquote>
    <p>Closes a <code>CInternetFile</code> and frees any of its resources.</p>
    
    <p>(sorry, I can't have three links)</p>
  </blockquote>
  
  <p>My gut assumption would be that since <code>CInternetSession::GetHttpConnection()</code> and <code>CHttpConnection::OpenRequest()</code> return pointers, and since the MSDN for <code>CHttpConnection</code> says </p>
  
  <blockquote>
    <p>You never create a <code>CHttpConnection</code> object directly; rather, call <code>CInternetSession::GetHttpConnection</code>, which creates the <code>CHttpConnection</code> object and returns a pointer to it.</p>
    
    <p>(sorry, I can't have three links)</p>
  </blockquote>
  
  <p>that <code>CInternetSession</code> internally stores a reference to the <code>CHttpConnection</code> that it generated and cleans up that object when <code>CInternetSession::Close()</code> is called. This is supported by <a href="https://msdn.microsoft.com/en-us/library/8yh4zs9e.aspx" rel="noreferrer">this MSDN article</a>, which does not mention any cleanup for the connection object, and states </p>
  
  <blockquote>
    <p>Dispose of the CInternetSession object --> Automatically cleans up open file handles and connections.</p>
  </blockquote>
  
  <h1>Short question</h1>
  
  <p>Is this necessary:</p>
  
  <pre><code>CInternetSession session(...);
  CHttpConnection * connection = session.GetHttpConnection(...);
  CHttpFile * file = connection-&gt;OpenRequest(...);
  
  ... Do stuff ...
  
  file-&gt;Close();
  delete file;
  
  connection-&gt;Close();
  delete connection;
  
  session.Close();
  </code></pre>
  
  <p>Or is it sufficient to do: </p>
  
  <pre><code>CInternetSession session(...);
  CHttpConnection * connection = session.GetHttpConnection(...);
  CHttpFile * file = connection-&gt;OpenRequest(...);
  
  ... Do stuff ...
  
  session.Close();
  </code></pre>
  
  <h2>Meta question</h2>
  
  <p>If---from the documentation for a library---it is unclear where the responsibility lies for cleaning up resources, what are possible ways to detect resource leaks? I know that Valgrind can be used for memory leaks, but what about file handles and other resources that might be tied up?</p>