Visual C++ GUI 应用程序陷入 MTA 模式
我有一个 C++ gui 项目表现出一些奇怪的行为。 在我的机器上,代码编译并运行得很好。 然而,在另一台机器上,代码可以编译,但最终以某种方式在 MTA 中运行。 显然,处于 MTA 中会导致 GUI 出现各种运行时问题。 这是我的 main:
[STAThreadAttribute]
int main(array<System::String ^> ^args)
{
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
Application::Run(gcnew Form1());
return 0;
}
我可以在 main 的第一行放置一个断点来检查单元状态,并且在正确构建/执行的计算机上,它将是“STA”,如预期的那样。 然而,在有问题的机器上,它将是“MTA”。 我什至可以尝试将公寓模式切换为 STA,但没有效果。
我尝试在编译之前删除调试/发布目录并清理项目,在没有附加调试器的情况下运行,但都无济于事。 我无法确定哪些机器可以工作,哪些不能。 如果我在工作机器上编译 exe 并将其带到有问题的机器上,它将正确执行,所以我怀疑这在某种程度上是构建环境问题。 所有涉及的机器都运行带有 Visual Studio 2008 标准的 Windows XP。 有任何想法吗?
I've got a C++ gui project exhibiting some strange behavior. On my machine, the code compiles and runs just fine. However, on another machine, The code compiles but ends up running in MTA somehow. Obviously, being in MTA causes all sorts of runtime problems for the GUI. Here is my main:
[STAThreadAttribute]
int main(array<System::String ^> ^args)
{
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
Application::Run(gcnew Form1());
return 0;
}
I can put a breakpoint on the first line of main to check the apartment state, and on the machines that build/execute correctly, it will be "STA", as expected. However, on the problematic machines, it will be "MTA". I can even try switching the apartment mode to STA, without effect.
I've tried removing the debug/release dirs and cleaning the project before compiling, running without a debugger attached, all to no avail. I can't determine any pattern to which machines work and which do not. If I compile the exe on a working machine and bring it over to a problematic machine, it will execute correctly, so I suspect this is somehow a build environment issue. All machines involved are running windows XP with visual studio 2008 standard. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
弄清楚了。 我们的项目正在从 openCV 调用 dll,并且该 dll 已针对多线程进行编译。 Visual Studio 注意到了这一点,并强制应用程序也针对多个线程进行编译。 一台机器与另一台机器上的 dll 版本不同是造成问题不规则的原因。
Figured it out. Our project was making calls to a dll from openCV, and that dll had been compiled for multiple threads. Visual studio noticed this and forced the app to compile for multiple threads as well. Differing versions of the dll from one machine to another were responsible for the irregularity of the problem.
我刚刚修复了 OpenCV 1.1 和托管代码中的类似错误。 由于某种原因,OpenCV 库似乎强制应用程序进入 MTA(可能是 OpenCV DShow 接口中的 COM 对象)。 无论如何,我找到了这个解决方案:
http://www.gamedev.net/community/ forums/mod/journal/journal.asp?userid=62708
如果您在主应用程序中重新初始化 COM,它应该会修复所有问题。 您需要链接到 OLE32.lib 才能使用 CoUninitialize()。 我使用了以下代码:
I just got done fixing a similar bug with OpenCV 1.1 and managed code. For some reason it seems that the OpenCV libraries force the application into MTA (perhaps a COM object in the OpenCV DShow interface). Anyway I found this solution:
http://www.gamedev.net/community/forums/mod/journal/journal.asp?userid=62708
If you re-initialize COM in your main application it should fix everything. You will need to link to OLE32.lib to use CoUninitialize(). I used the following code: