c ++ char * + std :: vector内存泄漏 [英] c++ char* + std::vector memory leak
问题描述
以下代码从磁盘读取一个大对象集合(95G的压缩对象,通过WriteObject流化器解压缩),并将其内容作为字符串打印。
对象.cxx:
std :: vector< char> ObjectHandler :: GetObject(const std :: string& path)
{
TFile * file = new TFile(path.c_str());
//如果文件未找到或为空
if(file-> IsZombie()){
cout< 找不到该对象在<路径<< endl;
}
//从根文件获取AliCDBEntry
AliCDBEntry * entry =(AliCDBEntry *)file-> Get(AliCDBEntry);
//创建一个输出缓冲区
TBufferFile * buffer = new TBufferFile(TBuffer :: kWrite);
//将AliCDBEntry对象流式并序列化到缓冲区
buffer-> WriteObject((const TObject *)entry);
//获取缓冲区的指针
char * pointer = buffer-> Buffer();
//将对象存储到引用的向量
std :: vector< char> vector(pointer,pointer + buffer-> Length());
//释放打开的文件
删除文件;
删除缓冲区;
return vector;
}
main.cxx:
ObjectHandler objHandler;
boost :: filesystem :: path dataPath(/ tmp);
boost :: filesystem :: recursive_directory_iterator endIterator;
if(boost :: filesystem :: exists(dataPath)&& boost :: filesystem :: is_directory(dataPath)){
for(static boost :: filesystem :: recursive_directory_iterator directoryIterator(dataPath); directoryIterator!= endIterator;
++ directoryIterator){
if(boost :: filesystem :: is_regular_file(directoryIterator-> status())){
cout < directoryIterator-> path()。string()<< endl;
std :: vector< char> vector = objHandler.GetObject(directoryIterator-> path()。string());
cout<<载体< endl;
}
}
}
正确的方法来实现这个方法?
2)这段代码是泄漏的,我怀疑是 char *指针
em>是由 ObjectHandler :: GetObject()方法返回的实际 std :: vector 。我用下面的代码测试了实现: struct sysinfo sys_info;
sysinfo(& sys_info);
cout<< Total:<< sys_info.totalram *(unsigned long long)sys_info.mem_unit / 1024< Free:<< sys_info.freeram *(unsigned long long)sys_info.mem_unit / 1024< endl;
,并且空闲ram连续减少,直到它达到0并且程序被终止。
内存泄漏是一个可以包含几个内容的术语;取决于你跟谁交谈。
一个是新的不匹配删除。
另一个,经常查看,是仍然引用和范围内,但只是没有使用或需要的内存。
如果你不使用profiller ,那么你不能确定你有什么,但是因为你有一个大的向量被传递,我们不知道你用它做什么,你可以做的第二,没有人会看到。 / p>
The following code is reading a big object collection (95G of compressed objects that are uncompressed via the WriteObject streamer) from disk and prints their content as strings.
object.cxx:
std::vector<char> ObjectHandler::GetObject(const std::string& path)
{
TFile *file = new TFile(path.c_str());
// If file was not found or empty
if (file->IsZombie()) {
cout << "The object was not found at " << path << endl;
}
// Get the AliCDBEntry from the root file
AliCDBEntry *entry = (AliCDBEntry*)file->Get("AliCDBEntry");
// Create an outcoming buffer
TBufferFile *buffer = new TBufferFile(TBuffer::kWrite);
// Stream and serialize the AliCDBEntry object to the buffer
buffer->WriteObject((const TObject*)entry);
// Obtain a pointer to the buffer
char *pointer = buffer->Buffer();
// Store the object to the referenced vector
std::vector<char> vector(pointer, pointer + buffer->Length());
// Release the open file
delete file;
delete buffer;
return vector;
}
main.cxx:
ObjectHandler objHandler;
boost::filesystem::path dataPath("/tmp");
boost::filesystem::recursive_directory_iterator endIterator;
if (boost::filesystem::exists(dataPath) && boost::filesystem::is_directory(dataPath)) {
for (static boost::filesystem::recursive_directory_iterator directoryIterator(dataPath); directoryIterator != endIterator;
++directoryIterator) {
if (boost::filesystem::is_regular_file(directoryIterator->status())) {
cout << directoryIterator->path().string() << endl;
std::vector<char> vector = objHandler.GetObject(directoryIterator->path().string());
cout << vector << endl;
}
}
}
1) Is calling by value the correct way to implement this method? Am i doing additional copies that could be avoided if calling by reference?
2) This code is leaking and i am suspecting that either the char *pointer is to blame, or the actual std::vector that is returned by the ObjectHandler::GetObject() method. I've tested the implementation with the following code:
struct sysinfo sys_info;
sysinfo (&sys_info);
cout << "Total: " << sys_info.totalram *(unsigned long long)sys_info.mem_unit / 1024 << " Free: " << sys_info.freeram *(unsigned long long)sys_info.mem_unit/ 1024 << endl;
and the free ram is continuously reduced, until it reaches 0 and the program is killed.
"Memory Leak" is a term that can encompass a few things; depending on who you talk to. One is a new without matching delete. The other, often looked over, is memory that's still referenced and in scope, but just not used or needed.
If you don't use a profiller, then you can't be sure which you have, but since you've a large vector being passed around, and we don't know what you do with it, you could be doing the 2nd and no one would ever see.
这篇关于c ++ char * + std :: vector内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!