radek-k / FFMediaToolkit

FFMediaToolkit is a cross-platform video decoder/encoder library for .NET that uses FFmpeg native libraries. It supports video frames extraction, reading stream metadata and creating videos from bitmaps in any format supported by FFmpeg.
MIT License
353 stars 56 forks source link

Disposing opened MediaFile crashes app #15

Closed bhutten14 closed 4 years ago

bhutten14 commented 4 years ago

I have this simple code and makes my app crash. I think it has something to do with the frames allocated memory. How do I handle this correctly, because I cannot dispose a frame?

            foreach (var file in files)
            {
                using (var mediaFile = MediaFile.Open(file))
                {
                    var frame = mediaFile.Video.ReadFrame(TimeSpan.FromMilliseconds(_frameTimeSpan));
                }
            }
radek-k commented 4 years ago

Thank you for reporting this issue. I will try to fix it in the next days. Your code is correct, ImageData objects are disposed automatically.

Claytonious commented 4 years ago

We are also having this issue. Can we provide any info that would help diagnose the problem?

Claytonious commented 4 years ago

BTW, if I comment out this line: https://github.com/radek-k/FFMediaToolkit/blob/master/FFMediaToolkit/src/Decoding/MediaFile.cs#L86, then the crash goes away. Of course this probably creates some memory leaks, but just mentioning it in case that makes it clearer where the problem may be.

Claytonious commented 4 years ago

This fixes the crash, but might be more heavy-handed than you prefer: https://github.com/radek-k/FFMediaToolkit/pull/17. EDIT: No, this is not a good fix after all. I closed it. This only fixed the crash because it stopped actually releasing the native pointers.

radek-k commented 4 years ago

I've just released the fixed v2.1.1 version on Nuget. I have deleted the ffmpeg.avcodec_free_context() call which can only be used manually in encoding. The ffmpeg.avformat_close_input() method calls it automatically.

Claytonious commented 4 years ago

Perfect - you just beat me to it: https://github.com/Claytonious/FFMediaToolkit/pull/1. ;-) Thank you!