Handle leaks in WinSock - WIndows Application (MFC)
-
Hi, We are developing an application in which for comminication to outside module we are using WinSock based sime socket approach. Our requirement is to make sure connection will always be on, so for that reason when ever we are getting disconnected or not able to connect to outside module we keep trying after every 1 minute. Our problem starts here we have observered that on every retry of socket reconnect it is leaking exact 2 windows handles, we have tried so many options but none of them are working. Following is the code that we are using right now,
bool CSocketClass::ConnectToServer(int nLineNo)
{
string strIPAddress;
int nPortNo;
SOCKET* l_ClientSocket;
int ConnectionResult;//---------------------- // Create a SOCKET for connecting to server if (nLineNo == 1) { m\_objLine1.m\_ClientSocket = socket(AF\_INET, SOCK\_STREAM, IPPROTO\_TCP); strIPAddress = m\_objLine1.m\_strIPAddress; nPortNo = m\_objLine1.m\_nPortNo; l\_ClientSocket = &(m\_objLine1.m\_ClientSocket); } else { m\_objLine2.m\_ClientSocket = socket(AF\_INET, SOCK\_STREAM, IPPROTO\_TCP); strIPAddress = m\_objLine2.m\_strIPAddress; nPortNo = m\_objLine2.m\_nPortNo; l\_ClientSocket = &(m\_objLine2.m\_ClientSocket); } if(INVALID\_SOCKET == \*l\_ClientSocket) { closesocket(\*l\_ClientSocket); return false; } //---------------------- // The sockaddr\_in structure specifies the address family, // IP address, and port of the server to be connected to. sockaddr\_in clientService; clientService.sin\_family = AF\_INET; clientService.sin\_addr.s\_addr = inet\_addr( strIPAddress.c\_str() ); clientService.sin\_port = htons( nPortNo ); //---------------------- // Connect to server. ConnectionResult = connect( \*l\_ClientSocket, (SOCKADDR\*) &clientService, sizeof(clientService) ) ; if (ConnectionResult == SOCKET\_ERROR) { if (nLineNo == 1) { //ERROR in line1 } else { //ERROR in line2 } closesocket(\*l\_ClientSocket); return false; } else //In case of successful connection { //Other actions } return true;
}
Can anyone help me? Thanks in advance.
-
Hi, We are developing an application in which for comminication to outside module we are using WinSock based sime socket approach. Our requirement is to make sure connection will always be on, so for that reason when ever we are getting disconnected or not able to connect to outside module we keep trying after every 1 minute. Our problem starts here we have observered that on every retry of socket reconnect it is leaking exact 2 windows handles, we have tried so many options but none of them are working. Following is the code that we are using right now,
bool CSocketClass::ConnectToServer(int nLineNo)
{
string strIPAddress;
int nPortNo;
SOCKET* l_ClientSocket;
int ConnectionResult;//---------------------- // Create a SOCKET for connecting to server if (nLineNo == 1) { m\_objLine1.m\_ClientSocket = socket(AF\_INET, SOCK\_STREAM, IPPROTO\_TCP); strIPAddress = m\_objLine1.m\_strIPAddress; nPortNo = m\_objLine1.m\_nPortNo; l\_ClientSocket = &(m\_objLine1.m\_ClientSocket); } else { m\_objLine2.m\_ClientSocket = socket(AF\_INET, SOCK\_STREAM, IPPROTO\_TCP); strIPAddress = m\_objLine2.m\_strIPAddress; nPortNo = m\_objLine2.m\_nPortNo; l\_ClientSocket = &(m\_objLine2.m\_ClientSocket); } if(INVALID\_SOCKET == \*l\_ClientSocket) { closesocket(\*l\_ClientSocket); return false; } //---------------------- // The sockaddr\_in structure specifies the address family, // IP address, and port of the server to be connected to. sockaddr\_in clientService; clientService.sin\_family = AF\_INET; clientService.sin\_addr.s\_addr = inet\_addr( strIPAddress.c\_str() ); clientService.sin\_port = htons( nPortNo ); //---------------------- // Connect to server. ConnectionResult = connect( \*l\_ClientSocket, (SOCKADDR\*) &clientService, sizeof(clientService) ) ; if (ConnectionResult == SOCKET\_ERROR) { if (nLineNo == 1) { //ERROR in line1 } else { //ERROR in line2 } closesocket(\*l\_ClientSocket); return false; } else //In case of successful connection { //Other actions } return true;
}
Can anyone help me? Thanks in advance.
I don't see any problem with this code. How do you use it? You don't need to call closesocket() if you know the socket is INVALID_SOCKET, and you can simplify your code a bit by using a pointer to m_objLine1 and m_objLine2 instead of copying all entries needed to local variables.
WhatEverType *pObjLine = (nLineNo == 1) &m_objLine1 : &m_objLine2;
if (INVALID_SOCKET == pObjLine->m_ClientSocket)
... -
I don't see any problem with this code. How do you use it? You don't need to call closesocket() if you know the socket is INVALID_SOCKET, and you can simplify your code a bit by using a pointer to m_objLine1 and m_objLine2 instead of copying all entries needed to local variables.
WhatEverType *pObjLine = (nLineNo == 1) &m_objLine1 : &m_objLine2;
if (INVALID_SOCKET == pObjLine->m_ClientSocket)
...Ya those things we have added just to check whether they could help us to reduce handle leaks but it dint help us. We also use pointer approach also but it dint help us either... This code is being used in one of the windows service which we use to activate manually..
-
Hi, We are developing an application in which for comminication to outside module we are using WinSock based sime socket approach. Our requirement is to make sure connection will always be on, so for that reason when ever we are getting disconnected or not able to connect to outside module we keep trying after every 1 minute. Our problem starts here we have observered that on every retry of socket reconnect it is leaking exact 2 windows handles, we have tried so many options but none of them are working. Following is the code that we are using right now,
bool CSocketClass::ConnectToServer(int nLineNo)
{
string strIPAddress;
int nPortNo;
SOCKET* l_ClientSocket;
int ConnectionResult;//---------------------- // Create a SOCKET for connecting to server if (nLineNo == 1) { m\_objLine1.m\_ClientSocket = socket(AF\_INET, SOCK\_STREAM, IPPROTO\_TCP); strIPAddress = m\_objLine1.m\_strIPAddress; nPortNo = m\_objLine1.m\_nPortNo; l\_ClientSocket = &(m\_objLine1.m\_ClientSocket); } else { m\_objLine2.m\_ClientSocket = socket(AF\_INET, SOCK\_STREAM, IPPROTO\_TCP); strIPAddress = m\_objLine2.m\_strIPAddress; nPortNo = m\_objLine2.m\_nPortNo; l\_ClientSocket = &(m\_objLine2.m\_ClientSocket); } if(INVALID\_SOCKET == \*l\_ClientSocket) { closesocket(\*l\_ClientSocket); return false; } //---------------------- // The sockaddr\_in structure specifies the address family, // IP address, and port of the server to be connected to. sockaddr\_in clientService; clientService.sin\_family = AF\_INET; clientService.sin\_addr.s\_addr = inet\_addr( strIPAddress.c\_str() ); clientService.sin\_port = htons( nPortNo ); //---------------------- // Connect to server. ConnectionResult = connect( \*l\_ClientSocket, (SOCKADDR\*) &clientService, sizeof(clientService) ) ; if (ConnectionResult == SOCKET\_ERROR) { if (nLineNo == 1) { //ERROR in line1 } else { //ERROR in line2 } closesocket(\*l\_ClientSocket); return false; } else //In case of successful connection { //Other actions } return true;
}
Can anyone help me? Thanks in advance.
How are you determining you have a memory leak? Is the MFC mechanism reporting leaks when you end your debug session? Are you storing MFC CObject derived classes within STL containers or vice versa? I've on occasion gotten false memory leak reports from MFC when mixing STL and MFC (when container classes are involved).
-
How are you determining you have a memory leak? Is the MFC mechanism reporting leaks when you end your debug session? Are you storing MFC CObject derived classes within STL containers or vice versa? I've on occasion gotten false memory leak reports from MFC when mixing STL and MFC (when container classes are involved).
-
Hi it seems you get it wrong. I am not talking about memory leaks, leaks I am talking about handle leaks. If I open task manager and look handle count for my application it used to increased by 2 at every connect trial...