MS Visual Studio Windows中的发布模式与调试模式 [英] Release mode Vs DEbug mode in MS Visual Studio Windows

查看:130
本文介绍了MS Visual Studio Windows中的发布模式与调试模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用MSVS 9(VS 2008).我的应用程序以及共享库(dll)(我正在用于与我的应用程序链接)也是c ++环境.现在观察以下情况:

I am using MSVS 9 (VS 2008). My application as well as shared library(dll)(I am using to link with my application) is also of c++ environment. Now observe the below cases:

  1. 当共享库/dll以调试模式构建且我的应用程序也以调试模式构建时 结果:应用程序成功执行

  1. when the shared library/dll is built in Debug mode and my application is also built in Debug mode Result: Application executed successfully

当共享库/dll在发布模式下构建并且我的应用程序也在发布模式下构建时 结果:应用程序成功执行

when the shared library/dll is built in Release mode and my application is also built in Release mode Result: Application executed successfully

当共享库/dll在发布模式下构建并且我的应用程序也在调试模式下构建时 结果:应用程序崩溃了,而没有从调用堆栈中加载任何符号.

when the shared library/dll is built in Release mode and my application is also built in Debug mode Result: Application is getting crashed without loading any symbols from call stack.

调用堆栈:

ntdll.dll!76e94684()
[下面的框架可能不正确和/或丢失,没有为ntdll.dll加载符号]

ntdll.dll!76e94684()
[Frames below may be incorrect and/or missing, no symbols loaded for ntdll.dll]

ntdll.dll!76e7d55f()
ntdll.dll!76e5fa18()
ntdll.dll!76e2b3c8()

ntdll.dll!76e7d55f()
ntdll.dll!76e5fa18()
ntdll.dll!76e2b3c8()

当我尝试在应用程序中使用以下SetName()和GetName()定义时,就会看到此问题.

This problem is seen when I am trying to use the following SetName() and GetName() definitions in my application.

    using namespace std;
    void main()
    { 
        Schema * schemaExp = new Schema();
        schemaExp -> SetName("ExpSchema");
        string srctable;
        srctable=schemaExp->GetName();
        cout <<"\nConnection EXPORT using the target table:" << srctable.c_str()  << endl;
        delete schemaExp;
    }

模式类定义:

    using namespace std;
    class Schema
    {
       public:
       TELAPI_EXPORT void   SetName(char *name); 
       TELAPI_EXPORT string     GetName(); 
      protected: 
       string tableName; 
    };
    void Schema::SetName(char *name)
    { 
       string str(name);
       tableName = str; 
    }
    string Schema::GetName()
    {
      return tableName;
    }

注意:上面只是我的应用程序的一部分,我的应用程序仅在#3中崩溃,并且可以在上述#1和#2情况下正常工作

Note: The above one is just a part from my application and my application is getting crash only in the #3 and working fine with #1 and #2 cases above

请帮助我解决此问题.任何帮助都将不胜感激.

Please help me in fixing this problem. Any kind of help is greatly appreciated.

谢谢.

推荐答案

当共享库/dll以发布模式构建并且我的应用程序也以调试模式构建时,结果:应用程序崩溃而未从调用堆栈中加载任何符号.

when the shared library/dll is built in Release mode and my application is also built in Debug mode Result: Application is getting crashed without loading any symbols from call stack.

这是因为这不是受支持的配置.默认情况下,调试"和发布"目标链接到CRT的不同版本,CRT以及其他版本使用不同的策略分配内存,并且彼此不兼容.

That's because this is not a supported configuration. By default, the Debug and Release targets link to different versions of the CRT, which (among other things) use different strategies for allocating memory and are not compatible with one another.

这只是更通用规则的扩展,您不应混合使用链接到CRT不同版本的库.所有项目都需要匹配.正如您已经看到的那样,当它们运行时,一切都将正常运行.

This is just an extension of the more general rule that you aren't supposed to mix libraries that link to different versions of the CRT. All of the projects need to match. And as you've already seen, when they do, everything works correctly.

有一些解决方法,但是要完成这些工作很多.本质上,您要确保所有内存分配都被隔离在单个DLL中,这样就不会跨越模块边界.您需要从DLL导出特定的函数以分配和释放内存,以确保分配内存的堆管理器与销毁内存的堆管理器相同.使用newdelete运算符时,您不能依靠这种情况.坦率地说,在这种情况下,我看不出所有这些努力如何为您带来任何有用的东西.

There are workarounds for this, but they're a lot of work to get right. Essentially, you make sure that all memory allocation is isolated within the single DLL so that nothing crosses module boundaries. You'll need to export specific functions from the DLL to allocate and free memory in order to ensure that the heap manager that allocates the memory is the same one that destroys it. You can't rely on this being the case when you use the new and delete operators. Frankly, in this case, I don't see how all this effort buys you anything useful.

请注意,这与是否启用优化无关(默认情况下,这些优化对于Release版本是启用的,而在Debug版本中则不启用).该设置与链接的CRT版本正交.恰好发生在"Debug"和"Release"目标隐含多个选项的情况下.您可以为一个项目打开优化,然后为另一个项目关闭优化,只要您确保它们都链接到相同版本的CRT,它就可以正常工作.但是,我再也看不到这样做的意义了.如果您要为一个启用优化,为什么要为另一个取消优化呢?

Do note that this is unrelated to whether or not optimizations are enabled (which they are by default for Release builds, and are not in Debug builds). That setting is orthogonal to the version of the CRT that is linked in. It just so happens that the "Debug" and "Release" targets imply multiple options. You could turn on optimizations for one project and turn them off for another and that should work, so long as you ensure that they both link to the same version of the CRT. But again, I can't really see the point in doing so…if you want optimizations enabled for one, why would you want them suppressed for the other?

相关:混合调试和发布库/二进制文件-不好的做法吗?

这篇关于MS Visual Studio Windows中的发布模式与调试模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆