How to Watch this Pointer – The Wizards Way!

30 07 2009


How to watch the this pointer? Just add ‘this’ to watch window. Everyone does like that. Isn’t it? But how Visual C++ wizards watch ‘this’ pointer? 😉

thiswizardway


The secret is, visual C++ compiler passes this pointer via ECX register. So add (ClassName*)(@ECX) to watch window will give you this pointer. Have a look at the screenshot.

thiswizardway2


Interesting, the internals of Visual C++. Isn’t it?


Targeted Audiance – Intermediate.

Advertisements




How to watch a range of values inside huge array?

3 07 2008


Assume you’ve an array with 1000 elements in it and you want to watch elements from 500 ~ 510, what will you do? One method is you can watch the array directly which will list all the 1000 variables and you’ve to scroll up to 500th element and have to watch. Is there any trick to watch just the range of 10 elements between 500 to 510?


Yes! Basically the idea is, you’ve to add – (Array+RangeStart)+RangeCount to the watch window. But there are different scenarios and obviously the trick varies slightly. See examples below.

1) If you have dynamically allocated array.
Check the code snippet.

// Array ptr with 1000 elements.
int* pArray = new int[1000];

// Initialize range 500 to 509 as 1.
for( int ArrayIndex = 500; ArrayIndex < 510; ++ArrayIndex )
{
    pArray[ ArrayIndex ] = 1;
}

In this case for watching range 500 ~ 510, you’ve to add – (pArray+500),10 to watch window. See the screenshot.

2) If you have array on stack.
If you’re array is on stack, if you add your array to watch window, the debugger automatically expands all elements, since it knows the max size of array. But we just want to see the range. See the code snippet.

// Array with 1000 elements.
int Array[1000] = { 0 };

// Initialize range 500 to 509 as 1.
for( int ArrayIndex = 500; ArrayIndex < 510; ++ArrayIndex )
{
    Array[ ArrayIndex ] = 1;
}

In this case for watching range 500 ~ 510, you’ve to add – ((int*)Array+500),10 to watch window. See the screenshot.


Watch window is a Treasure chest! isn’t it? 😉


Targeted Audience – Intermediate.





Watch heap objects eventhough the symbol went out of scope.

25 03 2008

Icon Description
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?

Icon How Can I Do It?
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.