简体   繁体   English

Valgrind将其检测为可能的内存泄漏

[英]Valgrind detect this as Possible Memory Leak

Below is the extract of Code, in which i am getting some Possible memory loss in Valgrind Report. 下面是代码的摘录,其中我在Valgrind Report中得到了一些可能的内存丢失。

681 int pbsc::PBSCAppMain( int argc, char **argv )
682 {
683     char pbscPath[255];
684     std::string configDir = "/../config/";
685     std::string pbscBinPath = getcwd(pbscPath,255);
686     std::string pbscConfigPath = pbscBinPath + configDir;
687
688
689
690     std::string localConfigFile = pbsc::GetFileNameFromDir(pbscConfigPath, PBSC_LOCAL_CONFIG_FILE);
691     if(false == localConfigFile.empty())
692     {
693         std::string localConfigFileWithPath = pbscConfigPath + localConfigFile;
694         ReadLocalConfiguration(localConfigFileWithPath);
695     }
696
697     std::string loggerConfigFile = pbsc::GetFileNameFromDir(pbscConfigPath, PBSC_LOGGER_CONFIG_FILE);
698     if(false == loggerConfigFile.empty())
699     {
700         std::string loggerConfigFileWithPath = pbscConfigPath + loggerConfigFile;
701         log4cxx::PropertyConfigurator::configureAndWatch(loggerConfigFileWithPath, 20000);
702     }
703

Below is the error what i am getting from Valgrind 以下是我从Valgrind得到的错误

==4594== 
==4594== 67 bytes in 1 blocks are possibly lost in loss record 754 of 1,141
==4594==    at 0x4A075BC: operator new(unsigned long) (vg_replace_malloc.c:298)
==4594==    by 0x5812CA8: std::string::_Rep::_S_create(unsigned long, unsigned long, std::allocator<char> const&) (new_allocator.h:104)
==4594==    by 0x581387A: std::string::_Rep::_M_clone(std::allocator<char> const&, unsigned long) (basic_string.tcc:629)
==4594==    by 0x5813913: std::string::reserve(unsigned long) (basic_string.tcc:510)
==4594==    by 0x58139B7: std::string::append(std::string const&) (basic_string.tcc:332)
==4594==    by 0x455446: std::basic_string<char, std::char_traits<char>, std::allocator<char> > std::operator+<char, std::char_traits<char>, std::allocator<char> >(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) (basic_string.h:2369)
==4594==    by 0x45ED5F: pbsc::PBSCAppMain(int, char**) (PBSCApp.cpp:686)
==4594==    by 0x45EC9B: main (PBSCApp.cpp:677)
==4594== 

My Question is when control leave this function why still memory is associated to this function? 我的问题是,当控件离开此功能时,为何仍将内存与该功能关联? I am calling this function number of times that's why my program size keeps on growing. 我多次调用此函数,这就是我的程序大小不断增长的原因。

Please suggest where exactly i am doing mistake. 请指出我到底在哪里做错。

Adding one more extract from Valgrind report that shows that even control return from the function but still memory taken freed. 从Valgrind报告中再添加一个摘要,该摘要显示即使该控件返回了该函数,但仍释放了内存。

==4594== 4,620 bytes in 110 blocks are possibly lost in loss record 1,092 of 1,141
==4594==    at 0x4A075BC: operator new(unsigned long) (vg_replace_malloc.c:298)
==4594==    by 0x5812CA8: std::string::_Rep::_S_create(unsigned long, unsigned long, std::allocator<char> const&) (new_allocator.h:104)
==4594==    by 0x581387A: std::string::_Rep::_M_clone(std::allocator<char> const&, unsigned long) (basic_string.tcc:629)
==4594==    by 0x5813913: std::string::reserve(unsigned long) (basic_string.tcc:510)
==4594==    by 0x5087CA2: log4cxx::helpers::Transcoder::decode(std::string const&, std::string&) (transcoder.cpp:248)
==4594==    by 0x503FA1A: log4cxx::LogManager::getLogger(std::string const&) (logmanager.cpp:120)
==4594==    by 0x503A498: log4cxx::Logger::getLogger(std::string const&) (logger.cpp:490)
==4594==    by 0x4204D0: FIELD_UT::InitializeLogger(unsigned int, int) (FIELD_UT.cpp:19)
==4594==    by 0x415074: BeamM::FIELD_UT(unsigned int, int, int, int&) (Beam.cpp:62)
==4594==    by 0x4158E0: BeamM::OnUplinkLlcPdu(rgw::pbscpmc::PMC-PBSC-MetaMessage const&, int&) (BeamM.cpp:134)
==4594==    by 0x425551: PBSC_SYS::OnUplinkLlcPdu(rgw::pbscpmc::PMC_PBSC_MetaMessage const&) (PBSC_SYS.cpp:135)
==4594==    by 0x4136A2: PBSCAppMain::HandleMessage(unsigned short, char const*, int) (PBSCAppMain.cpp:28)

I can't post all the code but can elaborate the sequence, hope i will be able to make it.... 我不能发布所有代码,但是可以详细说明序列,希望我能够做到。

  1. As shown in Valgrind report, PBSCAppMain() after receiving something on socket call, PBSC_SYS::OnUplinkLlcPdu. 如Valgrind报告中所示,PBSCAppMain()在套接字调用PBSC_SYS :: OnUplinkLlcPdu上收到内容后。
  2. PBSC_SYS::OnUplinkLlcPdu calls BeamM::OnUplinkLlcPdu. PBSC_SYS :: OnUplinkLlcPdu调用BeamM :: OnUplinkLlcPdu。
  3. BeamM::OnUplinkLlcPdu creates object for FIELD_UT. BeamM :: OnUplinkLlcPdu为FIELD_UT创建对象。
  4. from BeamM::OnUplinkLlcPdu, FIELD_UT::InitializeLogger is called to initialize its logger. 从BeamM :: OnUplinkLlcPdu中调用FIELD_UT :: InitializeLogger来初始化其记录器。 Here is the exact code line for that. 这是确切的代码行。

    void BeamM::UpdateUT() { shared_ptr pUt = nullptr; void BeamM :: UpdateUT(){shared_ptr pUt = nullptr; pUt = AddUT(lli); pUt = AddUT(lli); pUt->InitializeLogger(lli); pUt-> InitializeLogger(lli); } }

    void UT::InitializeLogger(unsigned int tlli, int beamId) { mLogger = log4cxx::Logger::getLogger("ut." + to_string(lli)); 无效UT :: InitializeLogger(unsigned int tlli,int beamId){mLogger = log4cxx :: Logger :: getLogger(“ ut。” + to_string(lli)); LOG4CXX_DEBUG(mLogger, "UT Logger created"); LOG4CXX_DEBUG(mLogger,“已创建UT Logger”); } }

Now if i am not wrong, after initilizing Logger control will be back then why in that case also, valgring says...Possibly leak. 现在,如果我没有记错的话,在初始化Logger控件后又会回来,那为什么在这种情况下,也会显示错误消息...可能是泄漏。

Thanks.. 谢谢..

it stays in while 1, continously keeps on checking data on socket and also keep reading the files 它停留在1的同时,继续检查套接字上的数据,并继续读取文件

Well if it stays in the loop forever (or, rather, until the process is forcibly terminated), then (within the context of the program) control never leaves the function. 好吧,如果它永远停留在循环中(或者,直到该过程被强制终止),那么(在程序的上下文中)控制就永远不会离开函数。

So when did you expect these resources to be freed? 那么,您何时期望释放这些资源? Valgrind doesn't know either. Valgrind也不知道。

This is a false positive; 这是一个假阳性; don't worry about it. 不用担心
(Notice that it says "possibly lost", not "definitely lost".) (请注意,它说的是“可能丢失”,而不是“绝对丢失”。)

try changing 尝试改变

char pbscPath[255];
std::string configDir = "/../config/";
std::string pbscBinPath = getcwd(pbscPath,255);
std::string pbscConfigPath = pbscBinPath + configDir;

to

char pbscConfigPath[300];
getcwd(pbscConfigPath,255);
strcat(pbscConfigPath,"/../config/");

Old school but you don't have to worry about hidden class stuff, (and it's faster too). 老派,但您不必担心隐藏的课程(而且速度也更快)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM