2013-04-17 37 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; 
    } 
} 

而且四條內存泄漏的堆棧是非常相似,所以這裏是其中之一:

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

命令(或其基類之一)有一個** virtual **析構函數嗎?我在這個問題中包含抽象基類和「接口」基礎。 'IOgreCommand'是什麼,'Command'與它有什麼關係? – WhozCraig

回答

3

如果IOgreCommand(或IOgreCommand派生自的任何類)沒有虛擬析構函數,則從IOgreCommand派生的任何類在刪除IOgreCommand指針時都不會調用其析構函數。

+0

好吧,我正在嘗試...... – droidpl

+0

我是否必須在每個命令上聲明一個析構函數(儘管我不會銷燬任何東西),或者只在基礎'IOgreCommand'類中聲明析構函數? – droidpl

+0

@Feantury只有一個很好。基本規則是:如果你期望通過指向基類的指針來處理類,那麼這個基類(最頂層的)應該有一個虛擬析構函數。 – Angew