Valgrind使用动态数组C ++进行内存泄漏

时间:2018-01-14 23:42:53

标签: c++ arrays dynamic memory-leaks valgrind

我已经尝试了几种方法来解决这个问题,但似乎无法解决这个问题。 Valgrind用我的resize方法指向内存泄漏,我觉得我可能会遗漏一些简单的东西。

.h文件

private:
  int* pArray;     // stores math expression
  int currentSize; // size of array

的.cpp

void Prog::resize(int newSize)
{
  cout << "Address of polynomial:\t\t\t" << &pArray << endl;
  int* temp = new int[newSize] {0};
  cout << "Address of temp:\t\t\t" << &temp << endl;
  copy(temp, pArray);
  delete[] pArray;
  pArray = temp;
  cout << "Address of pArray after swap:\t" << &pArray << endl;

  temp = nullptr;

  currentSize = newSize;
}

void Prog::copy(int* to, int* from)
  {
    for (int i = 0; i < currentSize; i++)
      to[i] = from[i];
  }

我添加了cout's以查看地址发生了什么,因为我认为将pArray交换到temp后会打印出temp的位置地址,但它似乎保留了原来的位置。是应该发生什么?

我尝试过创建一个交换方法,当我使用它时问题仍然存在。

void Prog::swap(int*& to, int*& from) 
{
  int* temp = to;
  to = from;
  from = temp;
} 

这是我运行程序和Valgrind时的输出。

程序片段

Address of pArray:                  0000006741EFF218
Address of temp:                    0000006741EFEEE8
Address of pArray after swap:       0000006741EFF218
D = +50x^20000 +15x^11 +5x^10 -12x^7 -4x^6 +30x^5 +4x^4 -2x^3 +50

Valgrind的

==22696== 24 bytes in 1 blocks are definitely lost in loss record 1 of 12
==22696==    at 0x4C2E80F: operator new[](unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==22696==    by 0x40230A: Prog::resize(int) (Prog.cpp:332)
==22696==    by 0x401DD7: Prog::operator=(Prog const&) (Prog.cpp:171)
==22696==    by 0x400DEE: main (lab1.cpp:36)
==22696==
==22696== 36 bytes in 1 blocks are definitely lost in loss record 2 of 12
==22696==    at 0x4C2E80F: operator new[](unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==22696==    by 0x40230A: Prog::resize(int) (Prog.cpp:332)
==22696==    by 0x401DD7: Prog::operator=(Prog const&) (Prog.cpp:171)
==22696==    by 0x400DD5: main (lab1.cpp:35)
==22696==
==22696== 52 bytes in 1 blocks are definitely lost in loss record 9 of 12
==22696==    at 0x4C2E80F: operator new[](unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==22696==    by 0x40230A: Prog::resize(int) (Prog.cpp:332)
==22696==    by 0x401DD7: Prog::operator=(Prog const&) (Prog.cpp:171)
==22696==    by 0x40201D: Prog::operator*=(Prog const&) (Prog.cpp:214)
==22696==    by 0x40116D: main (lab1.cpp:55)

感谢任何帮助!

编辑 - 赋值运算符也显示内存泄漏,但它使用了resize方法,这就是我将其遗漏的原因,但是这里请求了其余的代码:

Prog::Prog() : currentSize(1)
{
    pArray = new int[currentSize] {0};
}

Prog::~Prog()
{
    for (int i = 0; i < currentSize; i++)
      this->pArray[i] = 0;

    currentSize = 0;

    delete[] pArray;
    pArray = nullptr;
}


Prog& Prog::operator=(const Prog& rhs)
{
  if (this == &rhs)
    return *this;

  for (int i = 0; i < currentSize; i++)
    pArray[i] = 0;

  if (this->currentSize < rhs.currentSize)
  {
    resize(rhs.currentSize + 1);
    currentSize = rhs.currentSize;
    pArray = new int[currentSize];
    for (int i = 0; i < currentSize; i++)
      pArray[i] = rhs.pArray[i];
  }
  else
  {
    for (int j = 0; j < rhs.currentSize; j++)
      pArray[j] = rhs.pArray[j];
  }

  return *this;
}

1 个答案:

答案 0 :(得分:2)

operator=中,您调用resize,然后在接下来的两个语句中再次执行相同的操作。由于resize分配内存(并将指针存储到pArray中,然后您使用新的值覆盖operator=中的该值而不释放先前的值,则会出现泄漏。