unosquare / ffmediaelement

FFME: The Advanced WPF MediaElement (based on FFmpeg)
https://unosquare.github.io/ffmediaelement/
Other
1.18k stars 244 forks source link

Multiple instances of Media Element - Unmanaged references were left alive. #424

Closed jdwoolcock closed 5 years ago

jdwoolcock commented 5 years ago

Multiple instances of Media Element - Unmanaged references were left alive.

I have a simple WPF application that has multiple instances of the ffmediaelement player. When I close an instance and there are other instances running I get the error message. Unmanaged references were left alive. This is an indication that there is a memory leak. Is this actually a problem or just an erroneous error message.

Issue Categories

Version Information

Steps to Reproduce

I have attached a simple example FfmeTest that illustrates the problem. It has 4 media panels each one has a play and stop button. Start playing multiple instances and then press stop on one of the windows.

The following error message is logged

[34:18.002 | ERR | ReferenceCounter ] Unmanaged references were left alive. This is an indication that there is a memory leak. CodecContext - MediaComponent.cs; Line: 79, Member: .ctor - Instances: 2 SwrContext - AudioComponent.cs; Line: 112, Member: MaterializeFrame - Instances: 1 Packet - MediaPacket.cs; Line: 83, Member: CreateReadPacket - Instances: 22 SwsContext - VideoComponent.cs; Line: 233, Member: MaterializeFrame - Instances: 1

FfmeTest.zip

Commands to play and close

private RelayCommand<MediaElement> playCommand;
        public RelayCommand<MediaElement> PlayCommand
        {
            get
            {
                return playCommand
                       ?? (playCommand = new RelayCommand<MediaElement>(
                           async e => { await e.Open(new Uri("rtsp://184.72.239.149/vod/mp4:BigBuckBunny_115k.mov")); }));
            }
        }

        private RelayCommand<MediaElement> stopCommand;
        public RelayCommand<MediaElement> StopCommand
        {
            get
            {
                return stopCommand
                       ?? (stopCommand = new RelayCommand<MediaElement>(
                           async e => { await e.Close(); }));
            }
        }
mariodivece commented 5 years ago

FFMe has an internal reference counter for unmanaged allocations. This message is an indication that there is a real memory leak. Let me check out your code.

mariodivece commented 5 years ago

Ok, I see what the problem is. The reference counter keeps track of unmanaged references for all media container instances. When there are more media containers, this message appears. Note how the message does not appear when you close the last MediaElement. Since this meant for development diagnostics only, I have updated the message being logged to a more proper explanation.

Thanks for the report!