should a property be attributed as retain if it's returned from calling a C method by reference
-
22-06-2021 - |
Pergunta
For the following code (build without ARC)
in .h
@interface VideoFrameExtractor : NSObject {
AVFormatContext *pFormatCtx;
AVCodecContext *pCodecCtx;
}
in .m
int av_open_input_file(AVFormatContext **ic_ptr, const char *filename,
AVInputFormat *fmt,
int buf_size,
AVFormatParameters *ap);
// Open video file
if(av_open_input_file(&pFormatCtx, [moviePath cStringUsingEncoding:NSASCIIStringEncoding], NULL, 0, NULL)!=0)
goto initError; // Couldn't open file
// Retrieve stream information
if(av_find_stream_info(pFormatCtx)<0)
goto initError; // Couldn't find stream information
Should we set the attribute for the pFormatCtx property to be retain or something else? The reason asking this is that we got EXC_BAD_ACCESS error on referencing the property in the av_find_stream_info call.
Solução
Should we set the attribute for the pFormatCtx property to be strong or something else?
av_open_input_file
is not an Objective C method, and it allocates memory directly outside of ARC and without any kind of reference counting. So you definitely do not need handling those references through strong properties.
You should definitely look for it in the the way av_find_stream_info
may fail.
Actually, what I see is that you should follow some steps to correctly setup your library to work:
AVFormatContext* pFormatCtx = avformat_alloc_context();
avformat_open_input(&pFormatCtx, filename, NULL, NULL);
int64_t duration = pFormatCtx->duration;
// etc
avformat_free_context(pFormatCtx);
In any case, inspect the documentation and also have a look at this tutorial.