static类型指针使用中的问题(AVPicture 类型内存释放)

这段代码要实现的功能是对out->pict区域的内容进行保存,当下一帧刷新的时候释放上一帧的内容。代码段1是可以正常运行的,代码段2,当对pOutLastPic指向的内存空间进行访问的时候程序崩溃。区别在于,代码段2在else中先对out->pict进行释放,之后才保存。当然保存不到什么东西。问题出现的根源是对静态的out指针,指向的内存区域是同一块这个问题不够敏感。虽然名字变成pOutLastPic,但是指向内存区域仍然是一个。

代码段1:
status_t MediaPlayer::glrender(){
static AVPictureList *out = NULL;
struct timeval tv;
if (out == NULL)
{
out = (AVPictureList*)av_malloc(sizeof(AVPictureList));  
if (out == NULL)
{
mbDrawing = false;
return 0; 
}
}

mPictureQueue->get(out,true);

if (pOutLastPic == NULL)
{
Log::d(ANDROID_LOG_INFO,TAG,"pOutLastPic get first frame");
mOutLastPic = out->pict;
pOutLastPic = (AVPicture*)(&(mOutLastPic));
}else{
avpicture_free(pOutLastPic);
mOutLastPic = out->pict;
pOutLastPic = (AVPicture*)(&(mOutLastPic));
}
}

代码段2:
status_t MediaPlayer::glrender(){
static AVPictureList *out = NULL;
struct timeval tv;
if (out == NULL)
{
out = (AVPictureList*)av_malloc(sizeof(AVPictureList));  
if (out == NULL)
{
mbDrawing = false;
return 0; 
}
}

mPictureQueue->get(out,true);

if (pOutLastPic == NULL)
{
Log::d(ANDROID_LOG_INFO,TAG,"pOutLastPic get first frame");
//mOutLastPic = out->pict;
pOutLastPic = (AVPicture*)(&( out->pict));
}else{
avpicture_free(pOutLastPic);
//mOutLastPic = out->pict;
pOutLastPic = (AVPicture*)(&( out->pict));
}
}

你可能感兴趣的:(static类型指针使用中的问题(AVPicture 类型内存释放))