簡體   English   中英

使用 std::queue 的內存泄漏

[英]Memory Leakage with std::queue

我有一個多線程應用程序,一個消費者寫入隊列,生產者從中讀取。這是我的代碼;

//my_class.h

class My_class{

   My_class();

   double time;

   struct my_struct my_struct;

   unsigned char my_buffer[1000];

   My_class& operator=(const My_class &copy);

};


 //my_class.cpp

   My_class::My_class(){

      my_struct.mem1=30;

      my_struct.mem2=100;

      //other struct assignments.

      time=0.0;

      memset(buffer,0xEF,sizeof(buffer));

   }

   My_class& My_class::operator=(const My_class& copy){

       if(this!=&copy){

           my_struct.mem1=copy.my_struct.mem1;

           my_struct.mem2=copy.my_struct.mem2;

           memcpy_s((void*)buffer,copysize,(void*)copy.buffer,copy.buffer.copysize)

           time=copy.time;
       }
      return *this;
   } 

在主要有一個隊列;

 std::queue<My_class> my_queue;

 My_class my_class_variable;//Filled somewhere in thread1

  //thread1.cpp


   My_mutex.lock();

      my_queue.push_back(my_class_variable);

   My_mutex.unlock();

  //thread2.cpp


   My_mutex.lock();

      my_queue.pop();

   My_mutex.unlock();

當我分析代碼時,當我向隊列推送和彈出時,我看到大量內存泄漏。可能是什么問題?

添加一個應對構造函數。

My_class(const My_class &copy)
{
(*this) = copy;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM