views:

562

answers:

3

I have a AxWMPLib.AxWindowsMediaPlayer on a form. When I close the form, I get "Attempted to read or write protected memory. This is often an indication that other memory is corrupt." exception. It is OK with hiding the form but not with closing. Everything's fine when the component is removed from the form.

This is Winforms .Net3.5.

Any help appreciated.

A: 

ActiveX objects may have some sensitive dependencies on being closed in the correct order when the parent form is closed, otherwise they may go on living until gc runs - try looking through the interface for the control for any methods that look like they may have to do with closing, or destroying the object and calling those.

1800 INFORMATION
I've tried stopping, reseting source URL, closing, disposing but no luck. Can we get any closer to the problem by finding where the error originated? I haven't check event log yet.
David
A: 

Sometimes when working with ActiveX objects in .NET applications it is necessary to force garbage collection on exit. I generally do this in Form_Closing using:

GC.WaitForPendingFinalizers()
GC.Collect()

Also, if you have setup any event handlers for the object, you will want to disconnect them explicitly. I've found on a number of occasions that ActiveX objects will still remain active in the garbage bin and will attempt to call the event handler even after they have been disposed.

It may also be worth it to make sure playback has stopped before you try to dispose of the object.

Corin
A: 

This was happening to me, and it was when closing the form during a key press.

Seems the WMP control will cause problems if it has a key event to process.

Example with Form.KeyPreview = True

Sub Form_KeyDown(e As KeyEventArgs)
 AxWindowsMediaPlayer1.Dispose()
End Sub

Causes an Access Violation.

Sub Form_KeyDown(e As KeyEventArgs)
 e.Handled = True
 AxWindowsMediaPlayer1.Dispose()
End Sub

Closes cleanly, as the key press is blocked from reaching the WMP control.

Same thing happens when the form is closed as will dispose of the control.

WhoIsRich