2013-04-17 1 views
2

Visual C++ 2010에서 개발 중이며 "Visual Leak Detector"라는 라이브러리를 사용하여 메모리 누수를 확인합니다. 그런 다음 설명하고 해결할 수없는 것을 발견했습니다.클래스의 문자열로 인한 C++ 메모리 누수

ManageRenderListenerCommand::ManageRenderListenerCommand(string action):mAction(action){ 
} 

void ManageRenderListenerCommand::execute(){ 
    //Do something with action 
} 

헤더 파일은 다음과 같습니다 : 여기 내 코드입니다

class ManageRenderListenerCommand : public IOgreCommand{ 
private: 
    string  mAction; 
public: 
    ManageRenderListenerCommand(string action); 
    void execute(); 
}; 

UPDATE : 그것은 여기라고 :

void OgreMediator::onOgreChanged(AbstractOgreNegotiator* negotiator, NegotiatorEvent& 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->getLastEvent()); 
    } 

    //Execute the created command 
    if (command){ 
     command->execute(); 
     delete command; 
    } 
} 

그리고 4 개의 메모리 누수의 스택이 매우이다 유사하므로 여기에 그 중 하나가 있습니다.

---------- Block 163 at 0x023CEAE0: 8 bytes ---------- 
    Call Stack: 
    c:\program files\microsoft visual studio 10.0\vc\include\xmemory (36): CataractSimulator.exe!std::_Allocate<std::_Container_proxy> + 0x15 bytes 
    c:\program files\microsoft visual studio 10.0\vc\include\xmemory (187): CataractSimulator.exe!std::allocator<std::_Container_proxy>::allocate + 0xB bytes 
    c:\program files\microsoft visual studio 10.0\vc\include\xstring (469): CataractSimulator.exe!std::_String_val<char,std::allocator<char> >::_String_val<char,std::allocator<char> > + 0xA bytes 
    c:\program files\microsoft visual studio 10.0\vc\include\xstring (543): CataractSimulator.exe!std::basic_string<char,std::char_traits<char>,std::allocator<char> >::basic_string<char,std::char_traits<char>,std::allocator<char> > + 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=..... ........ 
+0

게시 한 코드에 메모리 누수가 없습니다. 문제 (있는 경우)는 다른 곳에 있습니다. – john

+0

그게 내가 생각하는거야! 이 클래스가 호출되는 곳을 알려줄 수 있습니다. – droidpl

+1

Command (또는 기본 클래스 중 하나)에 ** 가상 ** 소멸자가 있습니까? 나는 그 질문에 추상적 기초 클래스와 "인터페이스"베이스를 포함하고있다. 'IOgreCommand' 란 무엇이며,'Command'는 어떻게 그것과 관련이 있습니까? – WhozCraig

답변

3

IOgreCommand (또는 IOgreCommand에서 파생되는 모든 클래스)에 가상 소멸자가없는 경우 IOgreCommand에서 파생되는 클래스에는 IOgreCommand 포인터를 삭제할 때 소멸자가 호출되지 않습니다.

+0

그래, 내가 노력하고있어 ... – droidpl

+0

모든 명령이나 기본 IOgreCommand' 클래스에서만 소멸자를 선언해야 하나? – droidpl

+0

@Feantury 단 하나 괜찮습니다. 기본 규칙은 다음과 같습니다. 기본 클래스에 대한 포인터를 통해 클래스를 처리 할 것으로 예상되는 경우 기본 클래스 (최상위 클래스)에 가상 소멸자가 있어야합니다. – Angew