Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Handle leaks in WinSock - WIndows Application (MFC)

Handle leaks in WinSock - WIndows Application (MFC)

Scheduled Pinned Locked Moved C / C++ / MFC
helpc++sysadminquestion
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    chevu
    wrote on last edited by
    #1

    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.

    N B 2 Replies Last reply
    0
    • C chevu

      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.

      N Offline
      N Offline
      Niklas L
      wrote on last edited by
      #2

      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)
      ...

      home

      C 1 Reply Last reply
      0
      • N Niklas L

        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)
        ...

        home

        C Offline
        C Offline
        chevu
        wrote on last edited by
        #3

        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..

        1 Reply Last reply
        0
        • C chevu

          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.

          B Offline
          B Offline
          bob16972
          wrote on last edited by
          #4

          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).

          C 1 Reply Last reply
          0
          • B bob16972

            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).

            C Offline
            C Offline
            chevu
            wrote on last edited by
            #5

            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...

            B 1 Reply Last reply
            0
            • C chevu

              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...

              B Offline
              B Offline
              bob16972
              wrote on last edited by
              #6

              oops. sorry. my bad. :-O

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups