none
AudioVideoPlayback - GCHandle - Leak RRS feed

  • Question

  • I am currently leveraging the Microsoft.DirectX.AudioVideoPlayback namespace to play multiple videos. As video objects are instantiated and disposed, I noticed a small memory leak.

    The application needs to run weeks without restart and could instantiate and dispose several thousand videos.

    As the application instantiates and disposes each video object, it leaves a <Gchandle> behind. When I set the Video object's Owner property it creates the GCHandle which is a pointer from unmanaged direct X to the managed control the video plays on. After instantiating and disposing 25k videos, the application is consuming 1 gig of ram.

    I know all this from using an expensive Memory Profiler (.NET Memory Profiler).

    My question is how can I make this leak stop? I have a theory that the pointer back to the managed control is being pinned. My hope is there is a way to unpin it and have that resource recovered. I have recreated the <GCHandle> leak in another simple application. Here is a snippet and will cause a <GCHandle> leak:

        Private mobj_VIDEO As Video 'This is the actual Direct X video.
        Public Sub PlayVideo()
            Try
                mobj_VIDEO = New Video("C:\Fractals.avi")
                mobj_VIDEO.Size = New System.Drawing.Size(Me.PictureBox1.Width, Me.PictureBox1.Height)
                mobj_VIDEO.Owner = Me.PictureBox1
                mobj_VIDEO.Play()
                Application.DoEvents()
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
        End Sub

        Public Sub StopVideo()
            Try
                Me.mobj_VIDEO.Stop()
                Me.mobj_VIDEO.Owner = Nothing
                Me.mobj_VIDEO.Dispose()
                Me.Refresh()
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
        End Sub
    Friday, August 21, 2009 11:55 AM

Answers

  • The managed DirectX wrappers have been deprecated for a long time now.  You no doubt found a bug in them but getting it fixed is not in the cards.  XNA is its replacement, ask questions about it at forums.xna.com.  If you are really desperate to get it going, Reflector can show you what's inside the assemblies.  Good luck.

    Hans Passant.
    Friday, August 21, 2009 12:50 PM
    Moderator

All replies

  • The managed DirectX wrappers have been deprecated for a long time now.  You no doubt found a bug in them but getting it fixed is not in the cards.  XNA is its replacement, ask questions about it at forums.xna.com.  If you are really desperate to get it going, Reflector can show you what's inside the assemblies.  Good luck.

    Hans Passant.
    Friday, August 21, 2009 12:50 PM
    Moderator
  • Hey,

    using your code I've got the same problem and an application inspection using a profiler shows up the same results. Years ago I had a similar problem which could be solved when not resetting the Owner to nothing before calling the Dispose method:

    Public Sub StopVideo()
    	Try
    		Me.mobj_VIDEO.Stop()
    		Me.mobj_VIDEO.Dispose()
    		mobj_VIDEO.Owner = Nothing
    		Me.Refresh()
    	Catch ex As Exception
    		MessageBox.Show(ex.Message)
    	End Try
    End Sub
    With this code sample I do not recognize the problem any longer...

    Regards,
    Marcel
    Saturday, August 22, 2009 12:35 PM
  • I had nearly the same problem in a enterprise application wat show video throuthout a day. I solved it that way (just like Marcel Koester, but no setting to null so we have a some spare milliseconds at hand ;) :

                                video = new Video(fi.FullName);
                                video.Owner = videopanel;
                                videopanel.Width = screenW;
                                videopanel.Height = screenH;
                                video.Play();
                                while (clipend == false)
                                {
                                    if (video.CurrentPosition >= video.Duration)
                                        clipend = true;
                                }
                                clipend = false;                            
                                video.Dispose();




    If I don't set Owner property to null and it disposes perfectly just at next foreach iteration.
    Eugene Mikhaylov http://www.codelibrary.ru
    Monday, August 24, 2009 12:52 PM
  • Hi ofamous,

    Is the issue resolved?


    Hongye Sun [MSFT]

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Monday, August 31, 2009 3:20 AM
  • Hi ofamous,

    Is the issue resolved?


    Hongye Sun [MSFT]

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.

    Unfortunately, in spite of trying all the items suggested, the leak still occurs.

    I decided to test using the managed DirectShow namespace and instead of getting GC Handle leaks, I had an ever increasing unmanaged Heap.

    I decided to see if this issue extends to Microsoft's WMP ocx. It does. By loading and unloading several videos in succession, I get the same leak.

    I am doing a bit more comprehensive testing this week. I wiped my development system over the weekend. I want to try everything with a fresh install to ensure that it isn't a Codec issue. I am doubtful it is an issue with any of the directshow codecs because I have seen this leak on other machines also.

    If there was some way for me to purge the unmanaged heap without restarting the process I would be very happy.

    My Environment:
    Vista x64 Home Premium (SP2)
    Visual Studio 2008 SP1
    DirectX 9.0c

    CPU: i7-920 (2.66 Ghz)
    RAM: 9 gigs
    GPU: Dual 9800 GTX


    Thanks,

    Jim


    Monday, August 31, 2009 2:17 PM
  • Hi Jim,

    nobugz is right. MDX is currently deprecated in favour of XNA. I'd like to suggest you to use XNA instead and check if it has the same issue.

     

    Hongye Sun [MSFT]

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Tuesday, September 1, 2009 8:23 AM