简体   繁体   English

我的代码中是否存在 memory 泄漏? 用 valgrind 检查

[英]Is it memory leak in my code ? Checked with valgrind

I have the next dynamic allocation memory.我有下动态分配memory。

//before //前

RMIUpStream *rmiUpStream = RMIUpStream::create("RMIResources");

//after //后

boost::shared_ptr<RMIUpStream> rmiUpStream(RMIUpStream::create("RMIResources"));

//create function //创建function

RMIUpStream* RMIUpStream::create(const std::string& rmiResourceString)
{
  RMIUpStream* rmiUpStream = nullptr;

  DEBUG("RMIUpStream::create - ResourceString: "+  rmiResourceString);

  try {
    FileInputStream inCfg(SpecificBoundaryBasics::FILE_NAME_STRING);
    XML::Reader reader(inCfg);

    bool resourcesFound = false;

    while ((reader.getEventType() != XML::Reader::END_DOCUMENT) and (not resourcesFound)) {
      if (reader.getEventType() == XML::Reader::START_TAG) {
        if (reader.getName() == rmiResourceString) {
          resourcesFound = true;
        }
      }
      reader.nextEventType();
    }

    if (resourcesFound) {
      rmiUpStream = new RMIUpStream(rmiResourceString);
      boost::shared_ptr<GeneralRACOONStack::RACOONStackDownStreamBundle> downStreamBundle(
              GeneralRACOONStack::RACOONStackDownStreamBundle::create(*rmiUpStream, rmiResourceString));
      rmiUpStream->setDownStreamBundle(downStreamBundle);
    }
    else {
      throw XML::ValidityViolation("Could not found " + rmiResourceString, reader.getLineNumber());
    }
  }
  catch (XML::Violation& violation) {
    throw ErrorTypes::ResourceError(Outputs::FileParameters(std::string("RMIUpStream.") + SpecificBoundaryBasics::FILE_NAME_STRING,
                                                         violation.getLineNumber(),
                                                         violation.getDetail()));
  }

  return rmiUpStream;
}

I expect that the allocation of raw pointer in smart pointer will change the errors from valgrind.txt to disappear, but not.我希望在智能指针中分配原始指针会将错误从 valgrind.txt 更改为消失,但不会。

//valgrind.txt //valgrind.txt

==20363== 32,640 bytes in 1 blocks are possibly lost in loss record 2,403 of 2,406
==20363==    at 0x4028F03: malloc (vg_replace_malloc.c:298)
==20363==    by 0x40C968C: ??? (in /lib/libc-2.9.so)
==20363==    by 0x40C903E: iconv_open (in /lib/libc-2.9.so)
==20363==    by 0x8304484: xercesc_3_1::IconvGNUTransService::IconvGNUTransService(xercesc_3_1::MemoryManager*) (IconvGNUTransService.cpp:450)
==20363==    by 0x822407A: xercesc_3_1::XMLPlatformUtils::makeTransService() (PlatformUtils.cpp:483)
==20363==    by 0x8224391: xercesc_3_1::XMLPlatformUtils::Initialize(char const*, char const*, xercesc_3_1::PanicHandler*, xercesc_3_1::MemoryManager*) (PlatformUtils.cpp:271)
==20363==    by 0x80F77AA: RMIUpStream::RMIUpStream(std::string) (RMIUpStream.cpp:98)
==20363==    by 0x80F80CD: RMIUpStream::create(std::string const&) (RMIUpStream.cpp:68)
==20363==    by 0x810CC8E: main (main.cpp:171)

This is just one of many logos of this type.这只是这种类型的众多标志之一。 The memory that is possibly lost seems to increase.可能丢失的 memory 似乎增加了。

Your memory leak warning is probably created by the fact creating a shared pointer can throw.您的 memory 泄漏警告可能是由创建共享指针可能引发的事实造成的。 Imagine what happens if there is enough memory to make a RMIUpStream, but not enough to make a shared pointer to hold it.想象一下,如果有足够的 memory 来生成 RMIUpStream,但不足以生成共享指针来保存它,会发生什么。 The RMIUpStream object is created, but never tied to an object. RMIUpStream object 已创建,但从未绑定到 object。 This is why make_shared exists这就是make_shared存在的原因

See here for details详情见这里

---- UPDATE --- - - 更新 - -

What happens if you don't have make_shared?如果没有 make_shared 会怎样?

Firstly, be aware, this is complex stuff to do by hand, see here.首先,请注意,这是手工完成的复杂工作,请参见此处。 https://herbsutter.com/gotw/_102/ https://herbsutter.com/gotw/_102/

Use library code to do this for you!使用库代码为您执行此操作!

But actually your problem can be boiled down to this line of code...但实际上你的问题可以归结为这行代码......

  rmiUpStream = new RMIUpStream(rmiResourceString); // << not exception safe

Unless you put this in a smart pointer (like unique_ptr) then any exception thrown will loose this memory.除非你把它放在一个智能指针中(比如 unique_ptr),否则抛出的任何异常都会丢失这个 memory。 Simply put this in a boost::unique_ptr简单地把它放在 boost::unique_ptr

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

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