【问题标题】:LoaderLock was detected, and turning off the warning does not work检测到LoaderLock,关闭警告不起作用
【发布时间】:2011-02-03 18:47:57
【问题描述】:

我正在尝试编写一个应用程序,该应用程序从计算机上的默认录音设备接收声音。当运行从我的托管代码访问 DirectX 的任何代码时,我收到此错误:

DLL 'C:\Windows\assembly\GAC\Microsoft.DirectX.DirectSound\1.0.2902.0__31bf3856ad364e35\Microsoft.DirectX.DirectSound.dll' 正在尝试在 OS Loader 锁内进行托管执行。不要尝试在 DllMain 或图像初始化函数中运行托管代码,因为这样做会导致应用程序挂起。

DevicesCollection coll = new DevicesCollection();

Device d = new Device(DSoundHelper.DefaultCaptureDevice);

Capture c = new Capture(DSoundHelper.DefaultCaptureDevice);

都会导致 LoaderLock MDA 弹出并告诉我有问题。我已经在互联网(包括stackoverflow)上搜索了这个问题的解决方案,但大多数人只是说关闭警告,这不起作用。当我关闭警告时,会抛出一个通用的 ApplicationException,这甚至更没用。我也看到了this question 的答案,但这并没有帮助,因为他说要删除导致错误的代码。其他人说“修复你的代码”。

我的问题是:

如何从 C# 调用任何(最好是托管的)DirectX 代码而不出现此错误?

编辑:这是我得到的堆栈跟踪:

at Microsoft.DirectX.DirectSound.Device..ctor(Guid guidDev)
at Autotuner.fMain.button1_Click(Object sender, EventArgs e) in C:\\Users\\Scott\\Documents\\Visual Studio 2008\\Projects\\Autotuner\\Autotuner\\Form1.cs:line 17
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at Autotuner.Program.Main() in C:\\Users\\Scott\\Documents\\Visual Studio 2008\\Projects\\Autotuner\\Autotuner\\Program.cs:line 18
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()

【问题讨论】:

  • 例外是好的,发布 StackTrace。
  • 它的字面意思是 ApplicationException 并带有消息“应用程序中的错误”
  • Scott,你有没有解决过这个问题?我也面临同样的问题。

标签: c# directx loaderlock


【解决方案1】:

在托管环境之外,C++ 中在加载器锁中执行代码的最简单方法之一是让嵌入在 dll 中的类在全局范围内执行它们的初始化。

唯一一次初始化 dll 对象是在来自操作系统的 DllMain 消息期间 - 但在这些消息期间,加载程序锁定处于活动状态。加载器锁防止加载dll的不同线程同时进入单个dll的DllMain。

在 C++(可能是托管环境)中解决这个问题非常困难,因为可能有许多隐式对象需要在 dll 中构造。尽管如此,您需要找到从 DllMain 调用的初始化代码,并确保它是从 dll 导出的显式初始化/关闭函数调用的,或者是即时完成的。

【讨论】:

    猜你喜欢
    • 2010-10-27
    • 2012-10-05
    • 1970-01-01
    • 1970-01-01
    • 2017-03-06
    • 1970-01-01
    • 2014-05-03
    • 1970-01-01
    • 2010-10-29
    相关资源
    最近更新 更多