Please enable JavaScript to view this site.

Memory Validator Help

Navigation: Working with IIS and Services

Example Service Source Code

Scroll Prev Top Next More

 

Where to put your code

 

When you use the functions to load and unload Memory Validator from your service, it is important that you put the function calls in the correct place in your software.

 

The correct place to put them is in a 'balanced' location, such that you would expect no memory leaks to occur between the load and the unload function call, assuming the service was working correctly.

 

Typically, this means that Memory Validator is:

 

loaded as the first action in the service_main() function
 

unloaded just before the service control manager is informed of the stopped status
 

The source code shown below shows an example service_main() function used in a service, demonstrating where to load and unload Memory Validator.

 

The long comment covers problems with the way services are stopped and what may be displayed in a debugger if this happens.

 

seeAlsoThe code is extracted from service\service.cpp, part of the full example of an NT service, client and a utility for controlling whether the service uses Memory Validator.

 

 

doc-expand-iconShow the C++ example service_main() function

 

void serviceCallback(void   *userParam)
{
   // just tell the Service Control Manager that we are still busy
   // in this example userParam is not used
 
   static DWORD dwCheckPoint = 1;
   
   ssStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
   ssStatus.dwServiceSpecificExitCode = 0;
 
   ssStatus.dwControlsAccepted = 0;
      
   ssStatus.dwCurrentState = dwCurrentState;
   ssStatus.dwWin32ExitCode = dwWin32ExitCode;
   ssStatus.dwWaitHint = dwWaitHint;
   ssStatus.dwCheckPoint = dwCheckPoint++;
      
   // Report the status of the service to the service control manager.

 
   return SetServiceStatus(sshStatusHandle, &ssStatus);
}

 

void WINAPI service_main(DWORD   dwArgc, LPTSTR   *lpszArgv)
{
   if (bLogging)
   {
      svlMVStub_setLogFileName(SZLOGFILENAME);
      svlMVStub_deleteLogFile();
   }

 

  // register our service control handler:
 
  sshStatusHandle = RegisterServiceCtrlHandler(TEXT(SZSERVICENAME), service_ctrl);
  if (sshStatusHandle != 0)
  {
    DWORD   dwErr = 0;
 
    // **MV_EXAMPLE** start
 
#if _HONOUR_MV_MUTEX_LOAD
    if (bMemoryValidator)
#endif   //#if _HONOUR_MV_MUTEX_LOAD
    {
        // load Memory Validator (but if monitoring a 32 bit service with C++ Memory Validator x64 use svlMVStub_LoadMemoryValidator6432())
 
        if (bLogging)
        {
          svlMVStub_writeToLogFileW(_T("About to load C++ Memory Validator\r\n"));
        }
 
        SVL_SERVICE_ERROR   errCode;
#ifdef IS6432
        // x86 with x64 GUI
        errCode = svlMVStub_LoadMemoryValidator6432();
#else   //#ifdef IS6432
        // x86 with x86 GUI
        // x64 with x64 GUI
        errCode = svlMVStub_LoadMemoryValidator();
#endif   //#ifdef IS6432
        if (bLogging)
        {
          if (errCode != SVL_OK)
          {
              DWORD   lastError;
 
              lastError = GetLastError();
              svlMVStub_writeToLogFileW(_T("C++ Memory Validator load failed. \r\n"));
              svlMVStub_writeToLogFileLastError(lastError);
              svlMVStub_writeToLogFile(errCode);
 
              svlMVStub_dumpPathToLogFile();
          }
          else
          {
              svlMVStub_writeToLogFileW(_T("C++ Memory Validator load success. \r\n"));
          }
        }
 
        // setup a service callback so that the Service Control Manager knows the service
        // is starting up even if instrumentation takes longer than 10 seconds (which it will
        // for a non-trivial application)
 
        if (bLogging)
          svlMVStub_writeToLogFileW(_T("Setting service callback C++ Memory Validator\r\n"));
 
        errCode = svlMVStub_SetServiceCallback(serviceCallback,     // the callback
                                              NULL);           // some user data (we don't have any, so set NULL)
        if (bLogging)
        {
          if (errCode != SVL_OK)
          {
              svlMVStub_writeToLogFileW(_T("Setting service callback failed. \r\n"));
              svlMVStub_writeToLogFile(errCode);
          }
 
          svlMVStub_writeToLogFileW(_T("Starting C++ Memory Validator\r\n"));
        }
 
        errCode = svlMVStub_StartMemoryValidator();
        if (bLogging)
        {
          if (errCode != SVL_OK)
          {
              DWORD   lastError;
 
              lastError = GetLastError();
              svlMVStub_writeToLogFileW(_T("Starting C++ Memory Validator failed. \r\n"));
              svlMVStub_writeToLogFileLastError(lastError);
              svlMVStub_writeToLogFile(errCode);
          }
 
          svlMVStub_writeToLogFileW(_T("Finished loading C++ Memory Validator\r\n"));
        }
    }
#if _HONOUR_MV_MUTEX_LOAD
    else
    {
        if (bLogging)
          svlMVStub_writeToLogFileW(_T("Not using C++ Memory Validator, DLL will not be loaded\r\n"));
    }
#endif   //#if _HONOUR_MV_MUTEX_LOAD
 
    // **MV_EXAMPLE** end
 
    // SERVICE_STATUS members that don't change in example
     
    ssStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
    ssStatus.dwServiceSpecificExitCode = 0;
     
    // report the status to the service control manager.
     
    if (ReportStatusToSCMgr(SERVICE_START_PENDING, // service state
                            NO_ERROR,             // exit code
                            3000))                 // wait hint
    {
        // deliberately allocate some memory so we can see that
        // with Memory Validator
 
        char   *someLeakedMemory;
 
        someLeakedMemory = (char *)malloc((SIZE_T)3456);
 
        // do work
       
        dwErr = ServiceStart(dwArgc, lpszArgv);
 
        // finished doing work
    }
     
    // **MV_EXAMPLE** start
 
#if _HONOUR_MV_MUTEX_LOAD
    if (bMemoryValidator)
#endif   //#if _HONOUR_MV_MUTEX_LOAD
    {
        // unload Memory Validator here
        // IMPORTANT.
        // Because of the way services work, you can find that this thread which is trying to gracefully unload
        // MemoryValidator is ripped from under you by the operating system. This prevents Memory Validator from
        // removing all its hooks successfully. If Memory Validator does not remove all of its hooks successfully
        // because this happens, then you may get a crash when the service stops.
        //
        // An alternative fix is to spawn another thread which then unloads Memory Validator.
        // See the code for ServiceStop() for comments relating to this.
        //
        // A callstack for such a crash is shown below. If you see this type of crash you need to put your code to
        // unload Memory Validator somewhere else. The stack trace may be different, but a fundamental point is the
        // code calling through doexit(), exit() and ExitProcess()
        //
        //NTDLL! 77f64e70()
        //SVLMEMORYVALIDATORSTUB!
        //MSVCRT! 78001436()
        //MSVCRT! 7800578c()
        //DBGHELP! 6d55da25()
        //DBGHELP! 6d55de83()
        //DBGHELP! 6d53705d()
        //DBGHELP! 6d51cc69()
        //DBGHELP! 6d51f6e8()
        //DBGHELP! 6d524ebf()
        //DBGHELP! 6d52a7b0()
        //DBGHELP! 6d52b00a()
        //DBGHELP! 6d526487()
        //DBGHELP! 6d5264d7()
        //DBGHELP! 6d5264f7()
        //SVLMEMORYVALIDATORSTUB!
        //SVLMEMORYVALIDATORSTUB!
        //SVLMEMORYVALIDATORSTUB!
        //SVLMEMORYVALIDATORSTUB!
        //SVLMEMORYVALIDATORSTUB!
        //SVLMEMORYVALIDATORSTUB!
        //SVLMEMORYVALIDATORSTUB!
        //SVLMEMORYVALIDATORSTUB!
        //MSVCRT! 78001436()
        //MSVCRT! 780057db()
        //KERNEL32! 77f19fdb()
        //SVLMEMORYVALIDATORSTUB! ExitProcess hook
        //doexit(int 0x00000000, int 0x00000000, int 0x00000000) line 392
        //exit(int 0x00000000) line 279 + 13 bytes
        //mainCRTStartup() line 345
        //KERNEL32! 77f1b9ea()
 
        svlMVStub_UnloadMemoryValidator();
    }
 
    // **MV_EXAMPLE** end
 
    // try to report the stopped status to the service control manager.
     
    (VOID)ReportStatusToSCMgr(SERVICE_STOPPED, dwErr, 0);
  }
 
  return;
}