由于源文件中存在未执行的代码,运行应用程序时崩溃 - c++

发布于 2024-10-19 18:39:51 字数 936 浏览 12 评论 0原文

我正在研究一个非常棘手的问题,我已经研究了一周了。我撞到了一堵很硬的墙,我的额头撞得很痛,所以我希望有人能帮助我。

我在这个项目中使用 Visual Studio 2005 - 我安装了 2008,但在尝试时遇到了类似的问题。

我们目前有一个针对 OpenCv1.1 编译的应用程序,我正在尝试将其更新到 2.2。当我们切换到新库的静态链接时,应用程序崩溃 - 但仅限于发布模式。所以动态链接和调试都可以正常工作。

调用 push_back 时,崩溃发生在 std::vector 中。

然后我想出了一个示例测试应用程序,它在 opencv 中运行一些基本代码,运行良好,然后采用完全相同的代码并将其添加到我们的应用程序中。该代码失败。

然后我删除了应用程序,因此它没有实例化任何代码对象,除了主 gui 和 1 个调用该代码的类之外,它仍然崩溃了。但是,如果我直接在主 GUI 中运行该代码,它就可以正常工作。

然后我开始注释掉大量的应用程序(在永远不应该实例化的组件中),最终我一直向下直到......

我有一个具有方法的类

void Foo()  
{  
    std::vector<int> blah;  
    blah.begin();  
}  

如果在标头中定义了此方法,则测试代码可以工作,但如果此代码在 cpp 文件中定义,则会崩溃。另外,如果我使用 std::vector而不是 int,它也可以工作。

然后,我尝试使用编译器选项,如果我关闭了优化 (/Od) 并将内联函数扩展设置为仅 __inline (/Ob1),即使代码位于 cpp 文件中,它也可以工作。

当然,如果我们回到原始应用程序并自行更改这些编译器选项,它就会崩溃。

如果有人对此有任何见解,请告诉我。

谢谢, 利隆

I'm working on a pretty tricky problem that I've been on for literally a week now. I've hit a very hard wall and my forehead hurts from banging it so I'm hoping someone can help me out.

I am using Visual Studio 2005 for this project - I have 2008 installed but was running into similar issues when I tried it.

We have an application currently working compiled against OpenCv1.1 and I'm trying to update it to 2.2. When we switch over statically link to the new libs, the application crashes - but only in release mode. So dynamic linking and debug both work fine.

The crash is in std::vector when calling push_back.

I then came up with a sample test application which runs some basic code in opencv which works fine and then took that exact same code and added it to our application. That code fails.

I then gutted the application so it didn't instantiate any code objects except the main gui and 1 class which called that code and it still crashed. However, if I ran that code directly in the main gui, it worked fine.

I then started commenting out huge amounts of the application (in components that should never be instantiated) and eventually I worked my way down down down until...

I have a class that has a method

void Foo()  
{  
    std::vector<int> blah;  
    blah.begin();  
}  

If this method is defined in the header, the test code works, but if this code is defined in the cpp file, it crashes. Also, if I use std::vector<double> instead of int, it also works.

I then tried to play with the compiler options and if I have optimizations turned off (/Od) and Inline Function Expansion set to Only __inline (/Ob1) it works even with the code being in the cpp file.

Of course, if we go back to the ungutted application and change those compiler options by themselves, it crashes.

If anyone has any insights on this, please let me know.

Thanks,
Liron

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

葮薆情 2024-10-26 18:39:51

啊啊!解决办法想出来了。

在我们的解决方案中,我们定义了 _SECURE_SCL = 0,但在我们构建的第 3 方库中,这是未定义的(这意味着 = 1)。将 _SECURE_SCL 设置为 0 据说会大大减少运行时间,但必须在所有包含的库中执行相同的操作,否则它们将以不同的方式处理数组大小。

http://msdn.microsoft.com/en -us/library/aa985896%28v=vs.80%29.aspx

这是有趣的一周。

ARGH! Solution figured out.

In our solution we had defined _SECURE_SCL = 0, but in the 3rd party libs we had build, that was undefined (which means = 1). Setting _SECURE_SCL to 0 supposedly reduces runtimes drastically, but it has to be done the same across all included libs otherwise they will treat array sizes differently.

http://msdn.microsoft.com/en-us/library/aa985896%28v=vs.80%29.aspx

That was a fun week.

因为看清所以看轻 2024-10-26 18:39:51

STL 类(如向量<>)在发布版本和调试版本之间存在布局不匹配,这是由迭代器调试支持引起的。您的问题的行为与您在应用程序的发布版本中链接 .lib 或 DLL 的调试版本并在它们之间交换 STL 对象时遇到的麻烦完全相同。结果是堆损坏和访问冲突异常。

三次检查您的构建设置,并确保您仅链接发布构建中的 .libs 的发布构建和调试构建中的 .libs 的调试构建。

The STL classes, like vector<>, have a layout mismatch between the release and the debug builds, caused by iterator debugging support. Your problem behaves exactly like the kind of trouble you get into when you link a debug build of a .lib or DLL in the release build of your application and exchange an STL object between them. Heap corruption and access violation exceptions are the result.

Triple check your build settings and ensure that you only ever link the release build of the .libs in your Release build and the debug build of the .libs in your Debug build.

哽咽笑 2024-10-26 18:39:51

你可以尝试一下:

void Foo()  
{  
    std::vector<int> blah;
    blah.reserve(5);
    blah.begin();  
} 

could you try:

void Foo()  
{  
    std::vector<int> blah;
    blah.reserve(5);
    blah.begin();  
} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文