调试Release模式下遇到的问题 - Debug和Release的差异。
来源:互联网 发布:金兰软件大连有限公司 编辑:程序博客网 时间:2024/04/25 17:03
很多时候,代码在debug下没问题,但是跑到release下却出现问题,其实debug和relaese模式是有很多差别的,只有了解了这些差别,才能写出在release下也行为很好的程序出来。
原文地址:
1)http://www.codeproject.com/KB/debug/releasemode.aspx Debugging Release Mode Problems
2)http://blog.csdn.net/sjf331/archive/2005/04/06/338249.aspx windows中的调试
Overview
If you're reading this you've probably just discovered that you've made a classic developer mistake. You've been working under the assumption that your working Debug mode application will work in Release Mode. Now you know that there are no guarantees that debug code works in release mode.
If you're like me you've probably discovered this after months of development without ever building or testing a Release Mode version. This leads to our first rule:
Rule 1
The longer you wait to discover a release mode problem, the more difficult it will be to resolve. Build release mode versions often (at least once a week) and test them. This will save your behind if your schedule is tight.
Don't Remove Needed Code in Release Mode
This sounds pretty obvious, but is easy to do unintentionally. There are several macros that are commonly used that are completely removed when you compile in release mode. For example ASSERT macros and TRACE macros are quietly removed in Release builds. Any code that may be required, which was to be executed in an ASSERT or TRACE command will also be removed.
This leads to our second rule:
Rule 2 Rule 3
Making Debug Mode more like Release Mode
If your Release Mode problem is caused by code being unintentionally removed. It is very likely that you can reproduce the problem in debug mode.
Some problems can be caused by differences during preprocessor symbols used to compile Debug and Release projects. You can make your Debug compile more like a release compile by making the following changes:
- In Project Settings (Alt-F7) under the C++/C tab, set the category to "General" and change the "_DEBUG" preprocessor definition to "NDEBUG".
- Under the C++/C tab, set the category to "Preprocessor" and add the preprocessor definition "_DEBUG" to the "Undefined Symbols" edit field.
- Recompile using "Rebuild All"
If this recreates your problems in your Debug version then make the following changes to your code:
- Change all occurrences of ASSERT() to VERIFY().
- Look for any code you've placed inside of a "#ifdef _DEBUG" wrapper that might be needed in the Release version.
- Look for code in TRACE() must be executed. TRACEs like ASSERTs are compiled out in release mode.
If this fixes your problems in Debug mode, try rebuilding the Release mode version. It will very likely work this time.
Erroneous Assumptions
Are you assuming that your variables, and allocated data is initialized to some value (probably 0)? Are you assuming that all the resources that you've referred to in your code exist in your project? These bad assumptions can lead to problems between Debug and Release mode.
Rule 4 Rule 5
Variables and allocated memory are often initialized differently in Debug versions than in Release version of software. If you've assumed that your variables are zeroed when they are declared could easily cause strange behavior in Release mode on Win9x. You can also introduce bugs that are Win9x specific. WinNT, for security reasons, zeros all memory before it is used.
If you have references to resources that have been removed from your resource file, your debug version may work but your release version may crash.
Are You Listening to Your Compiler?
What is your compiler warning level set to? Often the warning level gets set to a ridiculously low level to reduce the noise in a build.
It's possible that by turning up the compiler warning level will expose your problem. I always set compiler warnings to either "Level 3" or "Level 4" depending on how masochistic I'm feeling. Go through and resolve every compiler warning (this is a very good idea to do before you release your code anyway). This can expose initialization problems and many other potential errors that may be causing your problem.
Rule 6
Last Resorts
Debugging in Release Mode
I've heard a myth repeated many times by my fellow VC++ developers. It is that it is not possible to debug in Release Mode. Fortunately, this is a myth.
Rule 7
It is possible to debug in Release mode. The first step is to turn on symbols:
- In Project Settings (Alt-F2) under the "C++/C tab" set the category to "General" and change the Debug Info setting to "Program Database".
- Under the "Link tab" check the "Generate Debug Info" tab.
- Recompile using "Rebuild All"
This will allow you to see the symbols in the release version. You might want to also consider the following:
- You may want disable your optimization settings when debugging your release version (though this isn't absolutely necessary). // added by zhangdl: 如果在release模式下调试,最好禁用优化,不然,源代码和执行路径不匹配!
- If you have trouble placing breakpoints, the command "__asm {int 3}" will cause your program to stop at that line (be sure to remove these lines from your program when you are done debugging).
Debugging in release mode has several limitations.
- The most annoying one is that you can't easily see into MFC because you don't have the symbols included for the MFC DLL.
- You must add symbols to the release versions of all libraries and DLL's you wish to debug in your release application.
Is the Compiler Generating Bad Code?
There is an outside chance that you've discovered a code generation problem in VC++. Frankly, folks often jump to this conclusion far too soon. To test this, simply turn off optimization in release mode.
If this fixes your problem you probably have used some questionable C++ coding practice. Believe it or not, it is possible to define C++ code that has some ambiguity in how it should be evaluated or that is just plain wrong but seems to work in some cases. For example this code will appear to work "correctly" in Debug Mode but not in Release Mode
#include <stdio.h>int* func1(){	int retval = 5;	return &retval;}int main(int argc, char* argv[]){	printf("%d/n", *func1());	return 0;}
I'm sure if you're creative or have read a few PC-lint ads that you could come up with similar examples.
Rule 8 Rule 9
BTW- the problem code shown above is noticed by the C++ compiler. If you had followed Rule 6 you would have resolved this before it was a problem.
In my personal experience, I've rarely seen a problem that really was bad code generation. My experience with VC++ 6.0 has been that the compiler occasionally asserts while generating code (especially when using templates), but I've not experienced any problem with my code associated with bad code generation. Installing service release 3 seems to have taken care of all the code generation asserts that I've seen in previous versions.
Final Thoughts
I've found that by adding a bit of rigor to my daily routine, that I've been able to avoid most last-minute Debug -v- Release mode problems. This is what I do.
- Check out files to be changed.
- Make changes, resolve all warnings, and then build both a Debug and Release version.
- Smoke-test both versions (this means I single-step through each new line of code, then I really try to break the code in the area that I've been working in).
- Fix any problems found.
- Check in my changes.
- Do a complete build using all currently check in code (this makes sure my code is integrated properly with everyone else code).
- Smoke-test my code again.
- Fix any more problems found (this may require backing out the checked in code for awhile)
- 调试Release模式下遇到的问题 - Debug和Release的差异。
- Debug和Release下调试的问题
- debug 和release 模式下的NSLog
- C++debug和release的差异
- Static变量在Debug和Release模式下的初始化顺序偶有差异
- [转]DEBUG和RELEASE 版本差异及调试相关问题
- DEBUG和RELEASE 版本差异及调试相关问题
- DEBUG和RELEASE 版本差异及调试相关问题
- DEBUG和RELEASE 版本差异及调试相关问题:
- DEBUG和RELEASE版本差异及调试相关问题
- DEBUG和RELEASE 版本差异及调试相关问题
- DEBUG和RELEASE 版本差异及调试相关问题
- DEBUG和RELEASE版本差异及调试相关问题
- DEBUG和RELEASE 版本差异及调试相关问题
- DEBUG和RELEASE 版本差异及调试相关问题
- Debug和Release的问题
- Debug & Release时遇到的问题
- 浮点型精度,数组越界,debug和release下运行的区别,release下的调试
- MD5算法之C#程序 MD5算法描述
- 如何写Windows下的批处理程序
- kb、kB、kbps、kBps的区别
- 女孩子最希望男友做八件事
- const使用详解(转帖)
- 调试Release模式下遇到的问题 - Debug和Release的差异。
- 在html中自定义标签(续)
- qt4的安装-应用程序设计及在arm板上的移植
- MyEclipse6.0 SQL Explorer连接SQL Server 2000数据库
- Java 文件管理类File
- 来成都的感受和收获
- C语言struct中的位域
- 游戏中运用脚本语言
- mount root filesystem的三种方式