malloc: *** error for object xx: pointer being freed was not allocate的问题的解决方法之一

最近使用ffmpeg解码中经常遇到如题的问题。

我的程序结构是这样的:

 -(void)recvData {

         NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

         、、、、省略部分

         [self  decodeVidio];

        、、、、省略部分

        [pool drain];

}

-(UIImage *)decodeVidio {

      、、、、

     AVFrame *pFrame = avcodec_alloc_frame();

     if (NULL == pFrame) {

        return nil;

      }

   、、、省略部分

   av_free(pFrame);     这里加上pFrame=NULL;

     、、、省略部分

}

  其实就在 av_free(pFrame); 加一行pFrame=NULL; 就解决此错误了。 原因分析:pFrame是在一个手动释放队列中申请的,在一帧数据解码完成后会释放掉。在下一帧数据进入解码的时候,又回重新分配内存空间,由于计算机缓存机制,系统又会使用原来那个内存指针。而刚好那块内存指针是个野指针。所以会提示标题的错误。

  解决方式:

  1. 加上pFrame=NULL; 不使用野指针

  2.pFrame做成成员变量。只分配一次。




你可能感兴趣的:(malloc: *** error for object xx: pointer being freed was not allocate的问题的解决方法之一)