title: C++ memory leaks with strings in a class
body: <p>I'm developing in Visual C++ 2010 and I'm using a library called "Visual Leak Detector" to check for memory leaks. Then I found one that I can't explain and solve.
  Here is my code:</p>
  
  <pre><code>ManageRenderListenerCommand::ManageRenderListenerCommand(string action):mAction(action){
  }
  
  void ManageRenderListenerCommand::execute(){
      //Do something with action
  }
  </code></pre>
  
  <p>The header file is:</p>
  
  <pre><code>class ManageRenderListenerCommand : public IOgreCommand{
  private:
      string      mAction;
  public:
      ManageRenderListenerCommand(string action);
      void execute();
  };
  </code></pre>
  
  <p><strong>UPDATE:</strong> It's called here:</p>
  
  <pre><code>void OgreMediator::onOgreChanged(AbstractOgreNegotiator* negotiator, NegotiatorEvent&amp; negotiatorEvent){
      IOgreCommand* command = NULL;
      if(negotiatorEvent.matchEvent("addToViewport")){
          command = new AddToViewportCommand(mCameraManager, mSceneCreator, mEngine);
      }else if (negotiatorEvent.matchEvent("manageRenderListener")){
          command = new ManageRenderListenerCommand(negotiatorEvent.getMessage());
      }else if (negotiatorEvent.matchEvent("manageMouseCamera")){
          command = new ManageMouseCameraCommand(mCameraManager, mMouseManager-&gt;getLastEvent());
      }
  
      //Execute the created command
      if (command){
          command-&gt;execute();
          delete command;
      }
  }
  </code></pre>
  
  <p>And the stacks of the four memory leaks are very similar, so here it's one of them:</p>
  
  <pre><code>---------- Block 163 at 0x023CEAE0: 8 bytes ----------
    Call Stack:
      c:\program files\microsoft visual studio 10.0\vc\include\xmemory (36): CataractSimulator.exe!std::_Allocate&lt;std::_Container_proxy&gt; + 0x15 bytes
      c:\program files\microsoft visual studio 10.0\vc\include\xmemory (187): CataractSimulator.exe!std::allocator&lt;std::_Container_proxy&gt;::allocate + 0xB bytes
      c:\program files\microsoft visual studio 10.0\vc\include\xstring (469): CataractSimulator.exe!std::_String_val&lt;char,std::allocator&lt;char&gt; &gt;::_String_val&lt;char,std::allocator&lt;char&gt; &gt; + 0xA bytes
      c:\program files\microsoft visual studio 10.0\vc\include\xstring (543): CataractSimulator.exe!std::basic_string&lt;char,std::char_traits&lt;char&gt;,std::allocator&lt;char&gt; &gt;::basic_string&lt;char,std::char_traits&lt;char&gt;,std::allocator&lt;char&gt; &gt; + 0x5F bytes
      c:\users\cps\desktop\surgery-sim\project\simulator\src\managerenderlistenercommand.cpp (10): CataractSimulator.exe!ManageRenderListenerCommand::ManageRenderListenerCommand
      c:\users\cps\desktop\surgery-sim\project\simulator\src\ogremediator.cpp (28): CataractSimulator.exe!OgreMediator::onOgreChanged + 0x53 bytes
      c:\users\cps\desktop\surgery-sim\project\simulator\src\abstractogrenegotiator.cpp (5): CataractSimulator.exe!AbstractOgreNegotiator::notifyMediator + 0x1C bytes
      c:\users\cps\desktop\surgery-sim\project\simulator\src\ogrerenderobserverregistry.cpp (37): CataractSimulator.exe!OgreRenderObserverRegistry::addListener + 0x15 bytes
      c:\users\cps\desktop\surgery-sim\project\simulator\src\ogremediator.cpp (73): CataractSimulator.exe!OgreMediator::addRenderListener
      c:\users\cps\desktop\surgery-sim\project\simulator\src\mousemanager.cpp (6): CataractSimulator.exe!MouseManager::startMouse
      c:\users\cps\desktop\surgery-sim\project\simulator\src\ogremediator.cpp (58): CataractSimulator.exe!OgreMediator::initFramework
      c:\users\cps\desktop\surgery-sim\project\simulator\src\simulatorapi.cpp (27): CataractSimulator.exe!SimulatorAPI::Facade::initFramework
      c:\users\cps\desktop\surgery-sim\project\simulator\src\simulatorapi.cpp (60): CataractSimulator.exe!SimulatorAPI::initFramework
      c:\users\cps\desktop\surgery-sim\project\simulator\src\loader.cpp (23): CataractSimulator.exe!Loader::Facade::initFramework + 0x16 bytes
      c:\users\cps\desktop\surgery-sim\project\simulator\src\loader.cpp (45): CataractSimulator.exe!Loader::go
      c:\users\cps\desktop\surgery-sim\project\cataractsimulator\src\cataractloader.cpp (5): CataractSimulator.exe!CataractLoader::go + 0x8 bytes
      c:\users\cps\desktop\surgery-sim\project\cataractsimulator\src\cataractloader.cpp (26): CataractSimulator.exe!main
      f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (555): CataractSimulator.exe!__tmainCRTStartup + 0x19 bytes
      f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (371): CataractSimulator.exe!mainCRTStartup
      0x7791ED6C (File and line number not available): kernel32.dll!BaseThreadInitThunk + 0x12 bytes
      0x77A1377B (File and line number not available): ntdll.dll!RtlInitializeExceptionChain + 0xEF bytes
      0x77A1374E (File and line number not available): ntdll.dll!RtlInitializeExceptionChain + 0xC2 bytes
    Data:
      14 4E 3D 02    00 00 00 00                                   .N=..... ........
  </code></pre>