【发布时间】:2020-04-13 09:38:17
【问题描述】:
我有下一个动态分配内存。
//之前
RMIUpStream *rmiUpStream = RMIUpStream::create("RMIResources");
//之后
boost::shared_ptr<RMIUpStream> rmiUpStream(RMIUpStream::create("RMIResources"));
//创建函数
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;
}
我希望在智能指针中分配原始指针会将错误从 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)
这只是这种类型的众多标志之一。可能丢失的内存似乎增加了。
【问题讨论】:
-
你好。内存泄漏检测/日志有时难以阅读。您可能想尝试使用 dr memory (drmemory.org)。
-
在您的代码中,不清楚谁拥有已使用 new 运算符分配的 rmiUpStream 的所有权。如果您不需要 shared_ptr(每个实例化类只有自己的所有者),您最好使用 std::unique_ptr。这将帮助您确定每个实例化类的所有者。
标签: c++ memory-leaks valgrind