C ++指针内存泄漏

时间:2015-08-18 18:25:06

标签: c++ memory

我刚刚开始学习c ++,我正面临一个`free():无效指针:错误。在gdb debug下,错误显示

  

[线程0xb7caab40(LWP 4405)退出]

     

*`/ home / a / Desktop / PathFinder2'出错:free():无效指针:0xb74a9b40 *

     

编程接收信号SIGABRT,已中止。 0xb7fdd424 in   __kernel_vsyscall()

在valgrid中

  

== 4487 == 1个块中的800个字节肯定会丢失1个丢失记录1

     

== 4487 ==在0x402ADFC:operator new [](unsigned int)(在/usr/lib/valgrind/vgpreload_memcheck-x86-linux.so中)

     

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

     

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

     

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

     

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

     

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

     

== 4487 ==

     

== 4487 ==泄漏摘要:

     

== 4487 ==绝对丢失:1个块中的800个字节

     

== 4487 ==间接丢失:0个块中的0个字节

     

== 4487 ==可能丢失:0个块中的0个字节

     

== 4487 ==仍然可以访问:0个块中的0个字节

     

== 4487 ==抑制:0个块中的0个字节

     

== 4487 ==

     

== 4487 ==错误摘要:来自2个上下文的2个错误(被抑制:0从0开始)

     

== 4487 ==

     

== 4487 ==上下文1中的1个错误:

     

== 4487 ==无效的free()/ delete / delete [] / realloc()

     

== 4487 ==在0x402B838:operator delete(void *)(在/usr/lib/valgrind/vgpreload_memcheck-x86-linux.so中)

     

== 4487 == by 0x804EF2E:__ nuu_cxx :: new_allocator :: deallocate(Point *,unsigned int)(在/ home / a / Desktop / PathFinder2中)

     

== 4487 == by 0x804E9F6:std :: _ Vector_base   :: _ M_deallocate(Point *,unsigned int)(in   /家/一个/桌面/ PathFinder2)

     

== 4487 == by 0x804E883:std :: _ Vector_base> ::〜_Vector_base()(in   /家/一个/桌面/ PathFinder2)

     

== 4487 == by 0x804E2F6:std :: vector> ::〜vector()(在/ home / a / Desktop / PathFinder2中)

     

== 4487 == by 0x804E25B:Assignm3 :: PathFinderResource :: ~PathFinderResource()(in   /家庭/ A /桌面/ PathFinder2)   == 4487 == by 0x41E83D0:__ run_exit_handlers(exit.c:82)

     

== 4487 == by 0x41E842C:exit(exit.c:104)

     

== 4487 == by 0x41CEA8A :(在主页下方)(libc-start.c:321)

     

== 4487 ==地址0x5768b40未堆叠,malloc'd或(最近)免费

     

== 4487 ==

     

== 4487 ==错误摘要:来自2个上下文的2个错误(被抑制:0从0开始)

这是我的编码

 select 
     id,
     cost*(price/(sum(price) over (partition by id)))
From
      table
  

块引用

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;
}
  

块引用

int main() 
{   
    AllocateProgramsVariableMemory();
    mazeObj->LoadMaze();
    string thread_Msg = "";
    string * thread_message = &thread_Msg;

    if (pthread_create(&myPFR.activeThreadArray[line1], NULL, solveMaze, thread_message) != 0) 
    {
        return EXIT_FAILURE;
    } 
    else 
    {
        cout << THREAD_NAMES[line1] << " has been created" << endl;
    }
 //---------------causing error--------------------------------------------
    if (pthread_create(&myPFR.activeThreadArray[line2], NULL, solveMaze, thread_message) != 0) 
    {
        return EXIT_FAILURE;
    } 
    else 
    {
        cout << THREAD_NAMES[line2] << " has been created" << endl;
    }
//------------------------------------------------------------------------ 
    if (pthread_join(myPFR.activeThreadArray[line1], NULL) != 0) 
    {   
        return EXIT_FAILURE;       
    }

    if (pthread_join(myPFR.activeThreadArray[line2], NULL) != 0) 
    {
        return EXIT_FAILURE;
    }

    DeallocateProgramsVariableMemory();
    return EXIT_SUCCESS;
}

1 个答案:

答案 0 :(得分:1)

static void *solveMaze(void *vptr_args)
{
  //...
  mazeObj->LoadMaze();
  //...
}

int main()
{
  static Maze * mazeObj;
  static Path * pathObj;
  static SubmitMazeSoln * submitMazeSolnObj;
  //...
}

参考上面的代码...... main中的mazeObj无法在solveMaze中具有范围。 solveMaze在哪里获得它的mazeObj。要么它不是相同的mazeObj,要么这不是真正的代码。

此致

沃纳

相关问题