title: FFMPEG Decoding - Memory Leak
body: <p>Developing an application for a test, I encountered an error. Meanwhile the packets were proccessed, I got a very horrible problem, a memory leak.</p>
  
  <p>The av_free_packet is applied correctly, I think (See the code). When I run the app, the memory grows up to 500MB meanwhile it's playing the audio file, that's not normal. VLC or WMplayer (Windows Media Player) just wastes 30/20mb reading that file.</p>
  
  <p>Here is the code:</p>
  
  <pre><code>static AVPacket pkt;
  static uint8_t *audio_pkt_data = NULL;
  static int audio_pkt_size = 0;
  static AVFrame frame;
  static bool first_time = true;
  
  if(first_time){
      first_time=false;
  }
  
  int len1, data_size = 0;
  
  for(;;){
      bool do_rt = false;
  
      while(audio_pkt_size &gt; 0){
          int obt_frame = 0;
  
          len1 = avcodec_decode_audio4(_audio_ccontext,&amp;frame,&amp;obt_frame,&amp;pkt);
          if(len1 &lt; 0){
              audio_pkt_size = 0;
              break;
          }
          audio_pkt_data+=len1;
          audio_pkt_size-=len1;
          if(obt_frame){
              data_size = av_samples_get_buffer_size(NULL,channel_count,sample_fr,_audio_ccontext-&gt;sample_fmt,1);
              memcpy((int16_t*)audio_buf,frame.data[0],data_size);
  
          }
          if(data_size &lt;= 0){
              continue;
          }
          do_rt = true;
      }
  
      if(pkt.data){
          //MessageBox(0,"hi","Hi",MB_OK); // This is only for test if the app si reaching this av_free_packet
          av_free_packet(&amp;pkt);
      }
  
      if(do_rt){
          return data_size;
      }
  
      // Try to get a new packet
      if(!audio_packets.empty()){
          WaitForSingleObject(Queue_Audio_Mutex,INFINITE);
              pkt = *audio_packets.front();
              audio_packets.pop();
          ReleaseMutex(Queue_Audio_Mutex);
  
          audio_pkt_size = pkt.size;
          audio_pkt_data = pkt.data;
      }else{
          return -1;
      }
  }
  return 0;
  }
  </code></pre>
  
  <p>I would appreciate your help. Thank you very much.</p>