Posts tagged Debugging
CRT Debug support – The Magic Memory values.
2![]()
Memory corruptions are every programmer’s nightmare. But Debug Heap provides some facility in debug build to help you to get rid of those memory corrupting problems. Depending to the type of memory allocation we have done, the debug heap will fill some magic value for the allocated memory contents. Take care that, this will be available only in debug build. Please see below.
![]()
- 0xCD – The memory locations filled with this magic number are allocated in heap and is not initialized.
- 0xFD – This magic number is known as “NoMansLand”. The debug heap will fill the boundary of the allocated memory block will this value. If you are rewriting this value, then it means, you are beyond an allocated memory block.
- 0xCC – The memory locations filled with this magic number means, it’s allocated in stack but not initialized. You can see this when you a variable on stack and look at its memory location. You can use /GZ compiler option to get the same feature in release build.
- 0xDD – The memory locations filled with this magic number are Released heap memory.
![]()
Regarding the 4th one – 0xDD, when I tried, the deleted memory locations are filled with 0xFEEE. I’ve to check it further. as per documentation its 0xDD.
![]()
Targeted Audience – Intermediate.
Locate implementation of a particular Dialog in huge Codebase Easily.
0![]()
Huge Maintenance projects usually contain a number of dialogs. Sometimes we want to modify a particular dialog in that project. During most of the case, we might be encountering that dialog for the first time( Guys digging old code base like me can understand it very well ). At that time, what usually people do to locate the dialog implementation is as follows.
- Iterate through all dialogs in the resource by opening it or by checking the dialog ids.
- Find the matching dialog.
- Take the dialog ID.
- Search the code base for reference to the Dialog ID.
- You found it!
If your code base have 100eds of dialogs, how much time will it take?
![]()
All dialogs have the same base class, CDialog. So the CDialog constructor will be called before the constructor of your dialog class get called. Now follow the steps.
- Start debugging your project by F5.
- Open the file – <VisualStudioDir>\VC98\MFC\SRC\DLGCORE.CPP
- Put breakpoints on all CDialog constructors. Note – there are many overloads for your CDialog constructor. Since its unknown how your dialog class is initialized, its better to put break points on all CDialog constructors.
- Now launch your dialog from your application and the debugger will break on the CDialog constructor.
- Now take the Call Stack by pressing Alt + 7.
- From the call stack you can identify the class you were searching.
![]()
The above steps only works for debug version. If you are in release version, then breakpoint at DLGCORE.CPP will be disabled at the starting itself. For that, see following steps.
1) Our problem is how to put breakpoint at CDialog in release build.
2) For that take any put any other dialog class in your workspace and put a breakpoint in its constructor and start debug by F5.
3) when the breakpoint at your dialog’s constructor is reached, right click and select “Go To Disassembly”. It will be as follows.
62: CDlg1Dlg::CDlg1Dlg(CWnd* pParent /*=NULL*/)
63: : CDialog(CDlg1Dlg::IDD, pParent)
64: {
00401216 push ebp
00401217 mov ebp,esp
00401219 push 0FFh
0040121B push offset $L87331 (00401d29)
00401220 mov eax,fs:[00000000]
00401226 push eax
00401227 mov dword ptr fs:[0],esp
0040122E push ecx
0040122F mov dword ptr [ebp-10h],ecx
00401232 mov eax,dword ptr [pParent]
00401235 push eax
00401236 push 66h
00401238 mov ecx,dword ptr [this]
0040123B call CDialog::CDialog (00401a0a)
00401240 mov dword ptr [ebp-4],0
4) From the disassembly you will get the address of CDialog constructor to be called.
5) now take the breakpoint window by pressing Ctrl+B.
6) Just add a breakpoint by specifying this memory location in “Break At” editbox. don’t forget to add 0x in front of memory location. here it will be 0x00401a0a.
7) Now you’ve successfully added a break point at CDialog constructor in release version too. Enable it only when you need it.
![]()
Targeted Audience – Beginners.
Trailing spaces after backslash delimiter – Programmers nightmare.
4![]()
Usually we used to have spaces after semi columns. Since C++ ignore whitespace, its not a problem. But in Visual C++ 6.0 that is not the case always. If you put spaces after the backslash delimiter(\), it will become one of the worst bugs that you encounter.
![]()
Usually we use backslash delimiter to concatenate lines – mostly while writing macros. See the following macro.
#define INCREMENT_AND_DECREMENT( Value ) \
Value = Value + 1; \ » » »
Value = Value - 1;
In the macro, instead of » char, put some tab or space and then compile. You will get a strange error as follows.
error C2501: 'Value' : missing storage-class or type specifiers
![]()
One of the defensive mechanism is – always enable “View whitespace” by pressing Ctrl+Shift+8. So take care next time and don’t spend hrs on these silly bugs as I did years before.
![]()
Note that its just a implementation behavior in Visual C++ 6.0 and may not found in other compilers or upcoming Visual C++ versions.
Thanks to Hemant for pointing out my implicit biasing towards VisualStudio 6.0. I forgot to specify that its just a Visual studio behavior.

Targeted Audience – Intermediate.
Watch heap objects eventhough the symbol went out of scope.
1![]()
While debugging we want to watch some objects through out. An object can be watched only with its symbol and it should be valid in the current stack frame. For instance,
CJobManager* CJobManager::GetJobManager
{
return m_pJobManager;
}
For watching the JobManager instance, we need the symbol m_JobManager in this stack frame. When the function leaves, we can’t see the JobManager instance anymore. So how can i watch these heap objects without the symbol in stack frame?
![]()
1) Get the address of your heap object. Just add to watch window and get the location. For instance assume its 0x00034de0.
2) Now add a new entry to the watch window like this – (CJobManager*)(0x00034de0)
3) Now eventhough your function returns and you loss the symbol, still you can see your heap object.
Really helpful isn’t it? i was fed up with the global objects in my project and atlast find this method to watch them premenently.
Hardcoding Breakpoint.
2![]()
In some cases, MSDEV wont allow to set break point event if we press F9 a number of times. In those situations you can hardcode a break point by yourself.
![]()
Just insert the following line to the point where you would like to break while executing.
__asm int 3;
![]()
VisualStudio uses Interrupt 3 for implement break points. When we put a breakpoint, actually the debugger is inserting an int 3 instruction at that point. While executing, when an interrupt 3 is raised – debugger understood that one breakpoint is reached and it breaks and shows the appropriate code.
Avoid Stepping into unwanted functions while debugging.
1![]()
While debugging we usually step into functions by using F11. But if the function have some CString parameters, the F11 will get into the CString constructor. This is a real headache while debugging. To avoid this, you can define a set of Step into ignorable functions in the autoexp.dat of Visual Studio.
![]()
1) The Autoexp.bat is in the following location.
<VisualStudioDir>\Common\MSDev98\Bin\AUTOEXP.DAT
2) For ignoring CString’s constructor and operator =, please add the following,
[ExecutionControl]
CString::CString=NoStepInto
CString::operator==NoStepInto
3) If you want to ignore all functions of a class then you can use,
[ExecutionControl]
CMyClass::*=NoStepInto
4) If you want to ignore all functions of a perticular namespace then you can use,
[ExecutionControl]
MyNameSpace::*=NoStepInto