2016-10-10 1 views
1

ich versuche, diesen Code Dauer einer Mediendatei zu erhalten:AVFormat lib avformat_free_context

AVFormatContext* pFormatCtx = NULL; 
avformat_open_input(&pFormatCtx, filename.c_str(), NULL, NULL); 

if (pFormatCtx != 0) { 
    avformat_find_stream_info(pFormatCtx, NULL); 

    int64_t media_duration = pFormatCtx->duration; 
    duration = media_duration/AV_TIME_BASE; 

    avformat_close_input(&pFormatCtx); 
    avformat_free_context(pFormatCtx); 
} 

Das Problem ist, wenn ich rufe avformat_free_context() die App sterben, wenn ich das Programm die Zeile aus kommentieren funktioniert, aber ich lieber Speicherleck vermeiden.

PS: ich rufe av_register_all() beim Programmstart auf.

Antwort

2

Weil avformat_close_input die AVFormatContext freigibt, nachdem es es geschlossen hat. Deshalb sollten Sie pFormatCtx per Zeiger senden.

So müssen Sie nicht anrufen avformat_free_context, wie es bereits von avformat_close_input genannt wird und keine Notwendigkeit, Speicherlecks zu kümmern.

Siehe Dokumentation für avformat_close_input als Referenz.