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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. CAsyncSocket::Close Crashes

CAsyncSocket::Close Crashes

Scheduled Pinned Locked Moved C / C++ / MFC
c++debugginghelpsysadminannouncement
7 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.
  • S Offline
    S Offline
    staticv
    wrote on last edited by
    #1

    Hey, im doing some client/server stuff in a windows service. Pretty much new to this stuff using Visual C++ 6 The problem I'm encountering is that when I try to stop the service through Service Manager, it crashes. I added some MessageBoxes code, to trace where they are crashing and I found that when it closes the listener socket it crashes!!! I tried to run the service as a console application, and by myself called the function which is called SERVICE__CONTROL__STOP event is received so that I may reproduce the bug and debug easily. But it is working fine. The windows service is only crashing when I stop it through Service Manager Here is some code The main function

    int main(int argc, char* argv[])
    {
    // Create the service object
    CTestService CustomServiceObject;

    if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
    {
        std::cerr << "MFC failed to initialize!" << std::endl;
        return 1;
    }
    
    // Parse for standard arguments (install, uninstall, version etc.)
    if (! CustomServiceObject.ParseStandardArgs(argc, argv)) 
    {
        // StartService() calls ::StartServiceCtrlDispatcher() 
    // with the ServiceMain func and stuff
        CustomServiceObject.StartService();
    }
    
    // When we get here, the service has been stopped
    return CustomServiceObject.m\_Status.dwWin32ExitCode;
    

    }

    The Service Handler callback function

    // static member function (callback) to handle commands from the
    // service control manager
    void CNTService::Handler(DWORD dwOpcode)
    {
    // Get a pointer to the object
    CNTService* pService = m_pThis;

    pService->DebugMsg("CNTService::Handler(%lu)", dwOpcode);
    switch (dwOpcode) {
    case SERVICE\_CONTROL\_STOP: // 1
        pService->SetStatus(SERVICE\_STOP\_PENDING);
        pService->OnStop();
    
    // ..
    // .. 
    // other event handling
    // ..
    // ..
    

    }

    the OnStop() function

    void CTestService::OnStop()
    {
    m_sListener.ShutDown(2);
    m_sConnected.ShutDown(2);

    MessageBox(NULL, "After Shutdown", NULL, IDOK); 
    
    m\_sConnected.Close();
    
    MessageBox(NULL, "Closed connected socket", NULL, IDOK); 
    
    // crashes here when I try to stop through service manager
    // but if I run as console application works fine and terminates successfully
    m\_sListener.Close();
    
    MessageBox(NULL, "Closed listener socket", NULL, IDOK); 
    
    ::PostThreadMessa
    
    I M 2 Replies Last reply
    0
    • S staticv

      Hey, im doing some client/server stuff in a windows service. Pretty much new to this stuff using Visual C++ 6 The problem I'm encountering is that when I try to stop the service through Service Manager, it crashes. I added some MessageBoxes code, to trace where they are crashing and I found that when it closes the listener socket it crashes!!! I tried to run the service as a console application, and by myself called the function which is called SERVICE__CONTROL__STOP event is received so that I may reproduce the bug and debug easily. But it is working fine. The windows service is only crashing when I stop it through Service Manager Here is some code The main function

      int main(int argc, char* argv[])
      {
      // Create the service object
      CTestService CustomServiceObject;

      if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
      {
          std::cerr << "MFC failed to initialize!" << std::endl;
          return 1;
      }
      
      // Parse for standard arguments (install, uninstall, version etc.)
      if (! CustomServiceObject.ParseStandardArgs(argc, argv)) 
      {
          // StartService() calls ::StartServiceCtrlDispatcher() 
      // with the ServiceMain func and stuff
          CustomServiceObject.StartService();
      }
      
      // When we get here, the service has been stopped
      return CustomServiceObject.m\_Status.dwWin32ExitCode;
      

      }

      The Service Handler callback function

      // static member function (callback) to handle commands from the
      // service control manager
      void CNTService::Handler(DWORD dwOpcode)
      {
      // Get a pointer to the object
      CNTService* pService = m_pThis;

      pService->DebugMsg("CNTService::Handler(%lu)", dwOpcode);
      switch (dwOpcode) {
      case SERVICE\_CONTROL\_STOP: // 1
          pService->SetStatus(SERVICE\_STOP\_PENDING);
          pService->OnStop();
      
      // ..
      // .. 
      // other event handling
      // ..
      // ..
      

      }

      the OnStop() function

      void CTestService::OnStop()
      {
      m_sListener.ShutDown(2);
      m_sConnected.ShutDown(2);

      MessageBox(NULL, "After Shutdown", NULL, IDOK); 
      
      m\_sConnected.Close();
      
      MessageBox(NULL, "Closed connected socket", NULL, IDOK); 
      
      // crashes here when I try to stop through service manager
      // but if I run as console application works fine and terminates successfully
      m\_sListener.Close();
      
      MessageBox(NULL, "Closed listener socket", NULL, IDOK); 
      
      ::PostThreadMessa
      
      I Offline
      I Offline
      includeh10
      wrote on last edited by
      #2

      many many my programs are running excellent in release but crashes at once in debug (I even can't see any interface) - I think this is microsoft's problem, we do not need to take responsibilities for it, I never worry about it at all. So take it easy.

      S 1 Reply Last reply
      0
      • I includeh10

        many many my programs are running excellent in release but crashes at once in debug (I even can't see any interface) - I think this is microsoft's problem, we do not need to take responsibilities for it, I never worry about it at all. So take it easy.

        S Offline
        S Offline
        staticv
        wrote on last edited by
        #3

        Well, if it crashes then there is a problem :)

        Top Web Hosting Providers[^] Do, or do not. There is no 'try'.

        1 Reply Last reply
        0
        • S staticv

          Hey, im doing some client/server stuff in a windows service. Pretty much new to this stuff using Visual C++ 6 The problem I'm encountering is that when I try to stop the service through Service Manager, it crashes. I added some MessageBoxes code, to trace where they are crashing and I found that when it closes the listener socket it crashes!!! I tried to run the service as a console application, and by myself called the function which is called SERVICE__CONTROL__STOP event is received so that I may reproduce the bug and debug easily. But it is working fine. The windows service is only crashing when I stop it through Service Manager Here is some code The main function

          int main(int argc, char* argv[])
          {
          // Create the service object
          CTestService CustomServiceObject;

          if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
          {
              std::cerr << "MFC failed to initialize!" << std::endl;
              return 1;
          }
          
          // Parse for standard arguments (install, uninstall, version etc.)
          if (! CustomServiceObject.ParseStandardArgs(argc, argv)) 
          {
              // StartService() calls ::StartServiceCtrlDispatcher() 
          // with the ServiceMain func and stuff
              CustomServiceObject.StartService();
          }
          
          // When we get here, the service has been stopped
          return CustomServiceObject.m\_Status.dwWin32ExitCode;
          

          }

          The Service Handler callback function

          // static member function (callback) to handle commands from the
          // service control manager
          void CNTService::Handler(DWORD dwOpcode)
          {
          // Get a pointer to the object
          CNTService* pService = m_pThis;

          pService->DebugMsg("CNTService::Handler(%lu)", dwOpcode);
          switch (dwOpcode) {
          case SERVICE\_CONTROL\_STOP: // 1
              pService->SetStatus(SERVICE\_STOP\_PENDING);
              pService->OnStop();
          
          // ..
          // .. 
          // other event handling
          // ..
          // ..
          

          }

          the OnStop() function

          void CTestService::OnStop()
          {
          m_sListener.ShutDown(2);
          m_sConnected.ShutDown(2);

          MessageBox(NULL, "After Shutdown", NULL, IDOK); 
          
          m\_sConnected.Close();
          
          MessageBox(NULL, "Closed connected socket", NULL, IDOK); 
          
          // crashes here when I try to stop through service manager
          // but if I run as console application works fine and terminates successfully
          m\_sListener.Close();
          
          MessageBox(NULL, "Closed listener socket", NULL, IDOK); 
          
          ::PostThreadMessa
          
          M Offline
          M Offline
          Moak
          wrote on last edited by
          #4

          Could you provide more information where it crashes in the debug build, including a stack trace? Is it your code or which line of the MFC source code causes the problem, perhaps it's an ASSERT? /M

          My webchat in Europe :java: (in 4K)

          S 1 Reply Last reply
          0
          • M Moak

            Could you provide more information where it crashes in the debug build, including a stack trace? Is it your code or which line of the MFC source code causes the problem, perhaps it's an ASSERT? /M

            My webchat in Europe :java: (in 4K)

            S Offline
            S Offline
            staticv
            wrote on last edited by
            #5

            Since, it is a Windows Service, and I'm using MSVC6, I'm really unable to debug it. When I call the Close method for CAsyncSocket class, the program crashes. So it seems that there is some problem in MFC's code.

            Top Web Hosting Providers[^] Do, or do not. There is no 'try'.

            M 1 Reply Last reply
            0
            • S staticv

              Since, it is a Windows Service, and I'm using MSVC6, I'm really unable to debug it. When I call the Close method for CAsyncSocket class, the program crashes. So it seems that there is some problem in MFC's code.

              Top Web Hosting Providers[^] Do, or do not. There is no 'try'.

              M Offline
              M Offline
              Moak
              wrote on last edited by
              #6

              If you are not able to attach a debugger to the process, how about a simulated stop of the service handler? Maybe write a mockup framework that would trigger your code under similar conditions. Just a guess, you are not using CAsyncSocket in multiple worker threads, everything is one thread context?

              My webchat in Europe :java: (in 4K)

              S 1 Reply Last reply
              0
              • M Moak

                If you are not able to attach a debugger to the process, how about a simulated stop of the service handler? Maybe write a mockup framework that would trigger your code under similar conditions. Just a guess, you are not using CAsyncSocket in multiple worker threads, everything is one thread context?

                My webchat in Europe :java: (in 4K)

                S Offline
                S Offline
                staticv
                wrote on last edited by
                #7

                I call the function which is called when the SERVICE_CONTROL_STOP event is fired by myself. But I'm unable to reproduce that crash. It only occurs when it is stopped by Service Manager. No I'm not using CAsyncSocket in multiple threads

                Top Web Hosting Providers[^] Do, or do not. There is no 'try'.

                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