我尝试使用windbg在进入成员函数时打印“this”指针,如下所示。
我使用VC的自带IDE进行调试,得到相同的观察结果。但是为什么呢?
我尝试使用windbg在进入成员函数时打印“this”指针,如下所示。
class IBase {
int m_i;
public:
IBase() :m_i(23) {}
virtual int FuncOne(void) = 0;
virtual int FuncTwo(void) = 0;
};
class DerivedOne : public IBase {
public:
virtual int FuncOne(void) { return 1; };//set break point here.
virtual int FuncTwo(void) { return 2; };
};
class DerivedTwo : public IBase {
public:
virtual int FuncOne(void) { return 101; };
virtual int FuncTwo(void) { return 102; };
};
void DoIt(IBase* Base)
{
int i=Base->FuncOne();//break point here
}
int main(int argc, char *argv[])
{
DerivedOne d1;
DerivedTwo d2;
DoIt(&d1);
DoIt(&d2);
return 0;
}
(1) 我使用VC2015调试版(32位)进行了编译。
(2) 我在"DoIt"函数中设置了断点。
(3) 当命中Base->FuncOne()时,我按下"F11"进入DerivedOne函数。
现在我可以看到调用堆栈如下:
0:000> k
# ChildEBP RetAddr
00 0041f654 0022157c ConsoleApplication1!DerivedOne::FuncOne [d:\documents\visual studio 2013\projects\consoleapplication1\consoleapplication1.cpp @ 13]
01 0041f734 0022173c ConsoleApplication1!DoIt+0x2c [d:\documents\visual studio 2013\projects\consoleapplication1\consoleapplication1.cpp @ 23]
02 0041f850 00221dc9 ConsoleApplication1!main+0x7c [d:\documents\visual studio 2013\projects\consoleapplication1\consoleapplication1.cpp @ 36]
03 0041f8a0 00221fbd ConsoleApplication1!__tmainCRTStartup+0x199 [f:\dd\vctools\crt\crtw32\dllstuff\crtexe.c @ 626]
04 0041f8a8 75b9338a ConsoleApplication1!mainCRTStartup+0xd [f:\dd\vctools\crt\crtw32\dllstuff\crtexe.c @ 466]
05 0041f8b4 77529902 kernel32!BaseThreadInitThunk+0xe
06 0041f8f4 775298d5 ntdll!__RtlUserThreadStart+0x70
07 0041f90c 00000000 ntdll!_RtlUserThreadStart+0x1b
但是 "dv" 命令给出了意外的结果。
0:000> dv
this = 0xcccccccc
为什么会出现这种情况?程序正常运行,在调试版本中没有进行任何优化,看起来一切都很好。但为什么“this”指针无效?我使用VC的自带IDE进行调试,得到相同的观察结果。但是为什么呢?
this
在Doit
函数中未定义:它不是你的任何一个类的成员。 - Jean-François Fabrereturn 1
放在单独的一行上,你会看到一个有效的this
。 - rustyx