C# System.AccessViolationException and System.Runtime.InteropServices.SEHException

牧云@^-^@ 提交于 2020-01-24 16:48:07

问题


We are creating a comic book application using C# and are having a mainForm created on startup as usual and then when pressing the '1' key a new form called detailForm pops up showing the details of the selected comic book. There is a also a third form that is created if you press '1' again on the detailForm that will bring up another form called comicForm that shows the actual comic book.

Our problem begins when you press '1' to go to the detailForm from the mainForm and then immediately press '2' to go back to the mainForm. When you do this, we get either of these exception. It is not consistent and can show either a System.AccessViolationException or a System.Runtime.InteropServices.SEHException.

We have a COM object on the detailForm that shows a youtube video specific to each comic book character.

There are 2 warnings being thrown when building the project each time:

1>  COM Reference 'AcroPDFLib' is the interop assembly for ActiveX control 'AxAcroPDFLib' but was marked to be linked by the compiler with the /link flag. This COM reference will be treated as a reference and will not be linked.

1>  COM Reference 'ShockwaveFlashObjects' is the interop assembly for ActiveX control 'AxShockwaveFlashObjects' but was marked to be linked by the compiler with the /link flag. This COM reference will be treated as a reference and will not be linked.

After it runs this code segment, it crashes each time:

private void detailForm_KeyDown_1(object sender, KeyEventArgs e)
    {
        if (e.KeyData == Keys.D2)
        {
            player.controls.stop();
            this.Close();
        }

        if (e.KeyData == Keys.D1)
        {
            this.Close();
            player.controls.stop();
            ComicForm comicShow = new ComicForm(newComic);
            comicShow.Show();
            comicShow.Focus();
        }
    }

After the crash, it highlights this in Program.cs, but doesn't actually run it:

Application.Run(new MainForm());

回答1:


If not already set, Find the reference of the ActiveX in your references of your visual studio project, select the reference and view it's properties via the properties window, look for the "Embed Interop Type" property and set it "to False".

This might fix the issue.



来源:https://stackoverflow.com/questions/20407868/c-sharp-system-accessviolationexception-and-system-runtime-interopservices-sehex

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!