当 AxWindowsMediaPlayer 关闭时,出现 AccessViolation 异常
我的表单上有一个 AxWMPLib.AxWindowsMediaPlayer
。 当我关闭表单时,我收到“尝试读取或写入受保护的内存。这通常表明其他内存已损坏。” 例外。 隐藏表单可以,但关闭则不行。 当组件从表单中删除时,一切都很好。
这是Winforms .Net3.5。
任何帮助表示赞赏。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这发生在我身上,而且是在按键期间关闭表单时。
如果 WMP 控件有一个关键事件需要处理,那么它似乎会引起问题。
Form.KeyPreview = True 的示例
会导致访问冲突。
完全关闭,因为按键被阻止到达 WMP 控件。
当窗体关闭时会发生同样的事情,因为将释放控件。
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
Causes an Access Violation.
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.
有时,在 .NET 应用程序中使用 ActiveX 对象时,有必要在退出时强制进行垃圾回收。 我通常在 Form_Closing 中使用以下方法执行此操作:
另外,如果您为该对象设置了任何事件处理程序,您将需要显式断开它们的连接。 我多次发现 ActiveX 对象在垃圾箱中仍然保持活动状态,并且即使在它们被处理后也会尝试调用事件处理程序。
在尝试处理该对象之前,确保播放已停止也可能是值得的。
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:
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.
ActiveX 对象可能对在父窗体关闭时以正确的顺序关闭有一些敏感的依赖关系,否则它们可能会继续存在,直到 gc 运行 - 尝试在控件的界面中查找看起来它们可能必须执行的任何方法关闭或销毁对象并调用它们。
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.
我想我已经有了:
只是在文档 http://msdn.microsoft.com/en-us/library/windows/desktop/dd562388(v=vs.85).aspx
我想我应该尝试一下。 现在看起来好多了。
I think I have it:
simply found the method on the doc http://msdn.microsoft.com/en-us/library/windows/desktop/dd562388(v=vs.85).aspx
I thought I'd give it a go. it seems much better now.