The MSDN documentation for CInternetSession::Close()
only says
Call this member function when your application has finished using the
CInternetSession
object.MSDN for CInternetSession::Close
For the CHttpConnection
object returned by CInternetSession::GetHttpConnection()
, and the CHttpFile
object returned by CHttpConnection::OpenRequest()
, do these objects have to be manually closed and deleted?
I cannot find the documentation for CHttpConnection::Close()
on MSDN, and CHttpFile
inherits its Close()
method from CInternetFile
; the documentation for which is similarly unhelpful:
Closes a
CInternetFile
and frees any of its resources.(sorry, I can't have three links)
My gut assumption would be that since CInternetSession::GetHttpConnection()
and CHttpConnection::OpenRequest()
return pointers, and since the MSDN for CHttpConnection
says
You never create a
CHttpConnection
object directly; rather, callCInternetSession::GetHttpConnection
, which creates theCHttpConnection
object and returns a pointer to it.(sorry, I can't have three links)
that CInternetSession
internally stores a reference to the CHttpConnection
that it generated and cleans up that object when CInternetSession::Close()
is called. This is supported by this MSDN article, which does not mention any cleanup for the connection object, and states
Dispose of the CInternetSession object --> Automatically cleans up open file handles and connections.
Is this necessary:
CInternetSession session(...);
CHttpConnection * connection = session.GetHttpConnection(...);
CHttpFile * file = connection->OpenRequest(...);
... Do stuff ...
file->Close();
delete file;
connection->Close();
delete connection;
session.Close();
Or is it sufficient to do:
CInternetSession session(...);
CHttpConnection * connection = session.GetHttpConnection(...);
CHttpFile * file = connection->OpenRequest(...);
... Do stuff ...
session.Close();
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?
The short answer is you don't have to call Close()
, it gets done by MFC destructors. Ideally the destructor should do all the necessary cleanups.
MFC classes are poorly documented as you noted. WinINet functions have much better documentation:
MSDN WinINet functions
It says for example, ::InternetOpen
handle must be closed with InternetCloseHandle
. We can look at definition for MFC classes and compare:
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\atlmfc\src\mfc\inet.cpp
CInternetSession(){
::InternetOpen //WinINet
...
}
CInternetSession::Close(){
::InternetCloseHandle //WinINet
...
}
CInternetSession::~CInternetSession(){
Close();
}
So we don't need to call internetSession.Close()
because it is done automatically by destructor. Calling it wouldn't hurt though. I guess if CInternetSession
is declared on heap or something, then we may need to Close()
because it might take sometime before destructor is called.
In the other example we don't have to call Close
either, but it doesn't hurt.
CHttpFile * file = connection->OpenRequest(...);
file->Close();//I think this is okay but not necessary because we "delete file" in next line
delete file;//calls Close(); and other necessary cleanups
Short version:
CInternetSession session(...);
CHttpConnection *connection = session.GetHttpConnection(...);
CHttpFile *file = connection->OpenRequest(...);
//... Do stuff ...
delete file;//don't skip
delete connection;//don't skip
//session.Close();//you can skip, this gets called when we exist the function
By the way, putting breakpoint on CInternetSession obj
and stepping in it, should also take you to the MFC file ..\VC\atlmfc\src\mfc\inet.cpp
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With