简体   繁体   中英

How can I plug this C++ memory leak?

I have a program that is giving the expected output, however I am receiving the following errors below my output"

* glibc detected * PathFinder2: free(): invalid pointer: 0xb6046b70 ***

I run my program using Valgrind. However i do not have a file called pthread_create.c or pthread_create.c .

How can I troubleshoot the error based on these stats by Valgrind:

==3439== 800 bytes in 1 blocks are definitely lost in loss record 100 of 103

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

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

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

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

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

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

Update Is there logic error in this part which lead to memory leak. I cant seem to find anything wrong.

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()中有new []而没有delete []。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM