简体   繁体   English

COM-Interop, EAccessViolation 在我退出应用程序后

[英]COM-Interop, EAccessViolation after i exit the Application

i am having a problem with a ActiveX i am using via COM-Interop.我通过 COM-Interop 使用的 ActiveX 有问题。 it`s throwing an exception after i exit my Application and i am not sure if this is my fault or the fault of the ActiveX.在我退出应用程序后抛出异常,我不确定这是我的错还是 ActiveX 的错。

is the the correct way to initilize and release an ActiveX via COM-Interop?是通过 COM-Interop 初始化和释放 ActiveX 的正确方法吗?

Error Message错误信息

错误消息

Sample Code which triggers the Exception触发异常的示例代码

public void CreateInvoice()
    {
        String path = @"";
        FaktNT.OLESrvClass OLESrv = null;

        try
        {
            OLESrv = new FaktNT.OLESrvClass();
            if (OLESrv.MandantLogin2() == 0)
            {
                try
                {
                    //Do Stuff
                }
                catch (System.Exception ex)
                {
                    //Log Error
                    throw;
                }
                finally
                {
                    OLESrv.MandantLogout();
                }
            }
            else
            {
                 //Do Stuff
            };
        }
        finally
        {
            System.Runtime.InteropServices.Marshal.ReleaseComObject(OLESrv);
            OLESrv = null;
            GC.Collect();
        }
    }

You should not need to release the COM object manually using Marshal.ReleaseComObject().您不需要使用 Marshal.ReleaseComObject() 手动释放 COM object。 This is done by .net automatically (depending on what you are doing to the reference counting in the native COM code of course).这是由 .net 自动完成的(当然取决于您对本机 COM 代码中的引用计数所做的操作)。

I would also try to check if the problem originates on the native side (for example in a destructor, which is called when the object is garbage collected).我还将尝试检查问题是否源自本机端(例如在析构函数中,当 object 被垃圾收集时调用)。

Is the COM dll generating any native threads, which may be running after the object was garbage collected? COM dll 是否生成任何本机线程,这些线程可能在 object 被垃圾收集后运行?

This is not a .NET message, the component itself is trapping the access violation exception.这不是 .NET 消息,组件本身正在捕获访问冲突异常。 Looks like it was written in Delphi, judging from the exception name.从异常名称来看,貌似是用Delphi写的。 Making native code bomb on an AV doesn't usually require a lot of help.在 AV 上制作原生代码炸弹通常不需要太多帮助。 But sure, you may be using the component 'incorrectly'.但可以肯定的是,您可能“错误地”使用了该组件。 You stubbed out too much code to really make an informed guess.您删除了太多代码,无法真正做出明智的猜测。 Other than that making calls on it in a finally block after you caught all exceptions that it might raise is a Bad Idea.除了在捕获所有可能引发的异常之后在 finally 块中调用它之外,这是一个坏主意。

Getting it to bomb on program exit instead of after the GC.Collect() call is not healthy either.让它在程序退出时而不是在 GC.Collect() 调用之后轰炸也不健康。 Sure sign that you haven't managed to call ReleaseComObject and null all the interface references.确定表明您没有设法调用 ReleaseComObject 和 null 所有接口引用。 That's common, better to leave it up to the garbage collector to do it right.这很常见,最好让垃圾收集器来做正确的事情。 Albeit that this message box is going to bomb the finalizer thread.尽管此消息框将轰炸终结器线程。 Yes, you probably need the vendor's help if a thorough code review doesn't help.是的,如果彻底的代码审查没有帮助,您可能需要供应商的帮助。

i solved the problem now.我现在解决了这个问题。 it was in fact me misusing the COM-Object, i missed to call OLESrv.EngineClose() which closes the COM-Object cleanly.实际上是我滥用了 COM-Object,我错过了调用 OLESrv.EngineClose() 来干净地关闭 COM-Object。

somehow this little piece of important informationen didn`t make it into the vendors documentation...不知何故,这条重要信息并没有出现在供应商文档中......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM