c++ char* + std::vector memory leak [closed]

2019-09-28 07:31发布

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.

1条回答
闹够了就滚
2楼-- · 2019-09-28 07:48

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

查看更多
登录 后发表回答