【问题标题】:How can I plug this C++ memory leak?我怎样才能堵住这个 C++ 内存泄漏?
【发布时间】:2015-08-15 18:07:34
【问题描述】:

我有一个给出预期输出的程序,但是我在输出下方收到以下错误"

* 检测到 glibc * PathFinder2: free(): invalid pointer: 0xb6046b70 ***

我使用 Valgrind 运行我的程序。但是我没有名为pthread_create.cpthread_create.c 的文件。

我如何根据 Valgrind 的这些统计数据排除错误:

==3439== 1 块中的 800 字节肯定丢失在丢失记录 100 中 103

==3439== at 0x402ADFC: operator new[](unsigned int) (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)

==3439== by 0x80560B5:Assignm3::Maze::InitMazeArray()(在 /home/a/Desktop/PathFinder2 中)

==3439== by 0x8055167:Assignm3::Maze::LoadMaze(std::string)(在 /home/a/Desktop/PathFinder2 中)

==3439== by 0x804A1F8:solveMaze(void*)(在 /home/a/Desktop/PathFinder2 中)

==3439== by 0x4052F6F: start_thread (pthread_create.c:312)

==3439== by 0x42A170D: 克隆 (clone.S:129)

更新 这部分是否存在逻辑错误导致内存泄漏。我似乎找不到任何问题。

static void *solveMaze(void *vptr_args)
{
  Point point1, point2, point3;
  int nxtPx, nxtPy;
  mazeObj->LoadMaze();
  point1 = mazeObj->getStartLocation();
  point3 = mazeObj->getEndLocation();
  VectorOfPointStructType Path, vecMain;
  Path.push_back(point1);
  vecMain.push_back(point1);
  point2 = mazeObj->getEndLocation();
  nxtPx = point1.getX();
  nxtPy = point1.getY();

  sleep(3);
  while (true)
  {
    string thread1, thread2;
    thread1 = THREAD_NAMES[line1];
    thread2 = THREAD_NAMES[line2];
    cout << "Thread " << THREAD_NAMES[line1] << " is running" << endl;
    cout << "Thread " << THREAD_NAMES[line2] << " is running" << endl;

    for (int x = 0; x < 5; x++)
    {
      if (thread1 != THREAD_NAMES[line1] && thread2 != THREAD_NAMES[line2])
      {
        cout << "Thread " << THREAD_NAMES[line1] << " is running" << endl;
        cout << "Thread " << THREAD_NAMES[line2] << " is running" << endl;
      }


      if (x == 0)
      {
        nxtPx++;
        point3 = Point(nxtPx, nxtPy);
        if ((mazeObj->IsThereBarrier(point3) || mazeObj->IsThereDanger(point3)) && !pathObj->isLocationInPath(point3, ob))
        {
          Path.push_back(point3);
          vecMain.push_back(point3);
          ob.push_back(point3);
          if (mazeObj->IsThereBarrier(point3))
          {
            pthread_mutex_lock(&mutex1);

            if (submitMazeSolnObj->submitPathToBarrier(pthread_self(), Path))
            {
              sleep(1);
            }
            pthread_mutex_unlock(&mutex1);
          }
          else if (mazeObj->IsThereDanger(point3))
          {
            pthread_mutex_lock(&mutex1);
            if (submitMazeSolnObj->submitPathToDangerArea(pthread_self(), Path))
            {

              if (thread1 == THREAD_NAMES[line1])
              {

                diemsg1();

              }
              else if (thread2 == THREAD_NAMES[line2])
              {

                diemsg2();

              }

              create();
            }
            pthread_mutex_unlock(&mutex1);
          }
          nxtPx--;
          Path.pop_back();
        }
        else if ((!mazeObj->IsThereBarrier(point3) && !mazeObj->IsThereDanger(point3)) && !pathObj->isLocationInPath(point3,
          vecMain))
        {
          point1 = point3;
          Path.push_back(point1);
          vecMain.push_back(point1);
          point3 = mazeObj->getEndLocation();
          x =  - 1;
          if (point1.isConnected(point2) || reachEnd)
          {
            reachEnd = true;
            break;
          }
        }
        else
        {
          nxtPx--;
        }

      }
      else if (x == 1)
      {
        nxtPy++;
        point3 = Point(nxtPx, nxtPy);
        if ((mazeObj->IsThereBarrier(point3) || mazeObj->IsThereDanger(point3)) && !pathObj->isLocationInPath(point3, ob))
        {

          Path.push_back(point3);
          vecMain.push_back(point3);
          ob.push_back(point3);

          if (mazeObj->IsThereBarrier(point3))
          {
            pthread_mutex_lock(&mutex1);

            if (submitMazeSolnObj->submitPathToBarrier(pthread_self(), Path))
            {
              sleep(1);
            }
            pthread_mutex_unlock(&mutex1);
          }
          else if (mazeObj->IsThereDanger(point3))
          {
            pthread_mutex_lock(&mutex1);
            if (submitMazeSolnObj->submitPathToDangerArea(pthread_self(), Path))
            {

              if (thread1 == THREAD_NAMES[line1])
              {

                diemsg1();

              }
              else if (thread2 == THREAD_NAMES[line2])
              {

                diemsg2();

              }

              create();
            }
            pthread_mutex_unlock(&mutex1);
          }
          nxtPy--;
          Path.pop_back();
        }
        else if ((!mazeObj->IsThereBarrier(point3) && !mazeObj->IsThereDanger(point3)) && !pathObj->isLocationInPath(point3,
          vecMain))
        {
          point1 = point3;
          Path.push_back(point1);
          vecMain.push_back(point1);
          point3 = mazeObj->getEndLocation();
          x =  - 1;
          if (point1.isConnected(point2) || reachEnd)
          {
            reachEnd = true;
            break;
          }
        }
        else
        {
          nxtPy--;
        }
      }
      else if (x == 2)
      {
        nxtPx--;
        point3 = Point(nxtPx, nxtPy);
        if ((mazeObj->IsThereBarrier(point3) || mazeObj->IsThereDanger(point3)) && !pathObj->isLocationInPath(point3, ob))
        {

          Path.push_back(point3);
          vecMain.push_back(point3);
          ob.push_back(point3);
          if (mazeObj->IsThereBarrier(point3))
          {
            pthread_mutex_lock(&mutex1);

            if (submitMazeSolnObj->submitPathToBarrier(pthread_self(), Path))
            {
              sleep(2);
            }

            pthread_mutex_unlock(&mutex1);
          }
          else if (mazeObj->IsThereDanger(point3))
          {
            pthread_mutex_lock(&mutex1);
            if (submitMazeSolnObj->submitPathToDangerArea(pthread_self(), Path))
            {

              if (thread1 == THREAD_NAMES[line1])
              {

                diemsg1();

              }
              else if (thread2 == THREAD_NAMES[line2])
              {

                diemsg2();

              }

              create();
            }
            pthread_mutex_unlock(&mutex1);
          }
          nxtPx++;
          Path.pop_back();
        }
        else if ((!mazeObj->IsThereBarrier(point3) && !mazeObj->IsThereDanger(point3)) && !pathObj->isLocationInPath(point3,
          vecMain))
        {
          point1 = point3;
          Path.push_back(point1);
          vecMain.push_back(point1);
          point3 = mazeObj->getEndLocation();
          x =  - 1;
          if (point1.isConnected(point2) || reachEnd)
          {
            reachEnd = true;
            break;
          }
        }
        else
        {
          nxtPx++;
        }
      }
      else if (x == 3)
      {
        nxtPy--;
        point3 = Point(nxtPx, nxtPy);
        if ((mazeObj->IsThereBarrier(point3) || mazeObj->IsThereDanger(point3)) && !pathObj->isLocationInPath(point3, ob))
        {

          Path.push_back(point3);
          vecMain.push_back(point3);
          ob.push_back(point3);

          if (mazeObj->IsThereBarrier(point3))
          {
            pthread_mutex_lock(&mutex1);

            if (submitMazeSolnObj->submitPathToBarrier(pthread_self(), Path))
            {
              sleep(2);
            }

            pthread_mutex_unlock(&mutex1);
          }
          else if (mazeObj->IsThereDanger(point3))
          {
            pthread_mutex_lock(&mutex1);


            if (submitMazeSolnObj->submitPathToDangerArea(pthread_self(), Path))
            {

              if (thread1 == THREAD_NAMES[line1])
              {

                diemsg1();

              }
              else if (thread2 == THREAD_NAMES[line2])
              {

                diemsg2();

              }

              create();
            }
            pthread_mutex_unlock(&mutex1);
          }
          nxtPy++;
          Path.pop_back();
        }
        else if ((!mazeObj->IsThereBarrier(point3) && !mazeObj->IsThereDanger(point3)) && !pathObj->isLocationInPath(point3,
          vecMain))
        {
          point1 = point3;
          Path.push_back(point1);
          vecMain.push_back(point1);
          point3 = mazeObj->getEndLocation();
          x =  - 1;
          if (point1.isConnected(point2) || reachEnd)
          {
            reachEnd = true;
            break;
          }
        }
        else
        {
          nxtPy++;
        }
      }
      else if (x == 4)
      {
        pthread_mutex_lock(&mutex1);

        for (;;)
        {

          point1 = Path[Path.size() - 2];
          nxtPx = point1.getX();
          nxtPy = point1.getY();
          point3 = Point(nxtPx - 1, nxtPy);

          if (!pathObj->isLocationInPath(point3, vecMain))
          {
            x = 0;
            Path.pop_back();

            if (mazeObj->IsThereBarrier(point3))
            {
              point3 = mazeObj->getEndLocation();
            }
            pthread_mutex_unlock(&mutex1);
            break;
          }
          point3 = Point(nxtPx, nxtPy + 1);

          if (!pathObj->isLocationInPath(point3, vecMain))
          {
            x =  - 1;
            Path.pop_back();

            if (mazeObj->IsThereBarrier(point3))
            {
              point3 = mazeObj->getEndLocation();
            }
            pthread_mutex_unlock(&mutex1);
            break;
          }
          point3 = Point(nxtPx + 1, nxtPy);

          if (!pathObj->isLocationInPath(point3, vecMain))
          {
            x = 2;
            Path.pop_back();

            if (mazeObj->IsThereBarrier(point3))
            {
              point3 = mazeObj->getEndLocation();
            }
            pthread_mutex_unlock(&mutex1);
            break;
          }

          point3 = Point(nxtPx, nxtPy - 1);

          if (!pathObj->isLocationInPath(point3, vecMain))
          {
            x = 1;
            Path.pop_back();

            if (mazeObj->IsThereBarrier(point3))
            {
              point3 = mazeObj->getEndLocation();
            }
            pthread_mutex_unlock(&mutex1);
            break;
          }
          Path.pop_back();
        }

        pthread_mutex_unlock(&mutex1);
        int deX = point1.getX();
        int deY = point1.getY();

        if (thread1 == THREAD_NAMES[line1])
        {

          time(&end);
          cout << "Thread: " << THREAD_NAMES[line1] << " found the dead end at " << "[ " << deX << " , " << deY << " ]" << endl;
          cout << "Time Elapsed: " << difftime(end, start) << " seconds" << endl;

        }
        else if (thread2 == THREAD_NAMES[line2])
        {


          time(&end);
          cout << "Thread: " << THREAD_NAMES[line2] << " found the dead end at " << "[ " << deX << " , " << deY << " ]" << endl;
          cout << "Time Elapsed: " << difftime(end, start) << " seconds" << endl;
        }
      }
      else
      {}
    }
    {
      break;
    }
  }
  Path.push_back(point2);
  if (!submitPath)
  {
    submitMazeSolnObj->submitSolutionPath(pthread_self(), Path);
    submitPath = true;
  }
  return NULL;
}

【问题讨论】:

  • 从堆栈跟踪的另一端开始。在Assignm3::Maze::InitMazeArray().
  • 你的solveMaze 函数有问题。
  • 你完全跳过了清单中提到你的函数的部分。如何向我们展示清单中提到的功能。
  • 嗨,我已经更新了帖子,我似乎找不到编码错误。
  • 学习将代码拆分为函数,这样更容易阅读和发现错误。

标签: c++ memory


【解决方案1】:

您在 Assignm3::Maze::InitMazeArray() 中似乎有没有 delete[] 的 new[]。

【讨论】:

    猜你喜欢
    • 2023-01-11
    • 2011-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-31
    相关资源
    最近更新 更多