gpt4 book ai didi

类中的字符串导致 C++ 内存泄漏

转载 作者:搜寻专家 更新时间:2023-10-31 01:49:48 26 4
gpt4 key购买 nike

我正在使用 Visual C++ 2010 进行开发,我正在使用一个名为“Visual Leak Detector”的库来检查内存泄漏。然后我发现了一个我无法解释和解决的问题。这是我的代码:

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();
};

更新:在这里调用:

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

最佳答案

如果 IOgreCommand(或 IOgreCommand 派生自的任何类)没有虚拟析构函数,则任何派生自 IOgreCommand 的类在删除 IOgreCommand 指针时都不会调用其析构函数。

关于类中的字符串导致 C++ 内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16059303/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com