如何插入此C ++内存泄漏?

时间:2015-08-15 18:07:35

标签: c++ memory

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

  

*检测到glibc * PathFinder2:free():无效指针:0xb6046b70 ***

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

如何根据Valgrind的这些统计信息排除错误:

  

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

     

== 3439 ==在0x402ADFC:operator new [](unsigned int)(在/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(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;
}

1 个答案:

答案 0 :(得分:1)

在Assignm3 :: Maze :: InitMazeArray()中似乎没有删除[]的新[]。