【c++基础】出现内存泄漏的一段代码

前言

 

code

float* lanemap = new float[output_h * output_w];
float* fsmap = new float[output_h * output_w];
cv::Mat showimg;
std::vector<PointProp> border_points;
CAN can_fs;
can_fs.canInit();
while(true)
{
  sem_wait(&sem_signal);
  mtx.lock();
  if(lane_maps.size()>0
    && fs_maps.size()>0
    && showimgs.size()>0
    && points_queue.size()>0)
  {
     memcpy(lanemap, lane_maps[0], output_h * output_w * sizeof(float));
     memcpy( fsmap, fs_maps[0], output_h * output_w * sizeof(float));
     showimg = showimgs.front();
     border_points = points_queue.front();
     lane_maps.erase( lane_maps.begin()); // pop_back();
     fs_maps.erase( fs_maps.begin()); // pop_back();
     showimgs.erase( showimgs.begin()); // pop_back();
     points_queue.erase(points_queue.begin()); // pop_back();
     lane_maps.shrink_to_fit();
     fs_maps.shrink_to_fit();
     showimgs.shrink_to_fit();
     points_queue.shrink_to_fit();
     mtx.unlock();
     sem_post(&sem_signal);
  }
  else
  {
    mtx.unlock();
    sem_post(&sem_signal);
    memset(lanemap, 0, output_h * output_w *sizeof(float));
    memset( fsmap, 0, output_h * output_w *sizeof(float));
    std::vector<PointProp>().swap(border_points);
    showimg.release();
    continue;
  }
  s = timeNow();
  can_fs.canData(border_points);
  can_fs.canSendFs();
  showSegMap(showimg, lanemap, fsmap, border_points);
  memset(lanemap, 0, output_h * output_w *sizeof(float));
  memset( fsmap, 0, output_h * output_w *sizeof(float));
  std::vector<PointProp>().swap(border_points);
  showimg.release();
}

注意:

红色部分的变量必须在while循环外部进行初始化,否则会造成内存泄漏;

 

posted on 2020-12-02 07:02  鹅要长大  阅读(216)  评论(0编辑  收藏  举报

导航