在網上很多討論Pure Virtual Function Called錯誤的文章,有的說了內存模型上的關系,有得則只是說了用例,以至于我當初只知道錯誤會發生,但不知道到底為何會發生.懵懂!現在讓我們從匯編語言結合C++對象模型來看個究竟.
我從網上趴了兩個例子代碼,具體看原文:http://blog.csdn.net/Blue_Dream_/archive/2008/04/08/2259649.aspx
#include <iostream>
using namespace std;

class Parent


{

public:
Parent()

{ }

~Parent()

{
cout << "Parent ~~~~~" << endl;
ClearALL();

}

void ClearALL()

{
cout << "ClearALL ~~~~~" << endl;
ThePure(); //調用自身的純虛函數,包裝一下是因為直接調用編譯器會識別出這樣調用是有問題的!

}

virtual bool ThePure() = 0 ;

};

class Child : public Parent


{

public:

Child()
{ }
virtual bool ThePure()

{
cout << "哈哈" << endl;
return false;
}
~Child()

{
ThePure();
}

};
int main()


{
Child c;
return 0;
}
程序退出前會調用Child類的析構函數,

Code
mov DWORD PTR $T8853[ebp], 0
lea ecx, DWORD PTR _c$[ebp]
call ??1Child@@QAE@XZ ; Child::~Child
來看看析構函數像什么樣子:
_TEXT SEGMENT
_this$ = -4
??1Child@@QAE@XZ PROC NEAR ; Child::~Child, COMDAT
; File test.cpp
; Line 40
push ebp
mov ebp, esp
push ecx
mov DWORD PTR _this$[ebp], ecx
mov eax, DWORD PTR _this$[ebp]
mov DWORD PTR [eax], OFFSET FLAT:??_7Child@@6B@ ; Child::`vftable'
; Line 41
mov ecx, DWORD PTR _this$[ebp]
call ?ThePure@Child@@UAE_NXZ ; Child::ThePure
; Line 42
mov ecx, DWORD PTR _this$[ebp]
call ??1Parent@@QAE@XZ ; Parent::~Parent
mov esp, ebp
pop ebp
ret 0
??1Child@@QAE@XZ ENDP ; Child::~Child
_TEXT ENDS
這個函數不是重點,只是能看到析構函數里調用了基類的析構函數:
call ??1Parent@@QAE@XZ ; Parent::~Parent
再來看看Parent類的析構函數的樣子:
_TEXT SEGMENT
_this$ = -4
??1Parent@@QAE@XZ PROC NEAR ; Parent::~Parent, COMDAT
; File test.cpp
; Line 12
push ebp
mov ebp, esp
push ecx
mov DWORD PTR _this$[ebp], ecx
mov eax, DWORD PTR _this$[ebp]
mov DWORD PTR [eax], OFFSET FLAT:??_7Parent@@6B@ ; Parent::`vftable'
; Line 13
push OFFSET FLAT:?endl@std@@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@1@AAV21@@Z ; std::endl
push OFFSET FLAT:??_C@_0O@HGFA@Parent?5?5?$HO?$HO?$HO?$HO?$HO?$AA@ ; `string'
push OFFSET FLAT:?cout@std@@3V?$basic_ostream@DU?$char_traits@D@std@@@1@A ; std::cout
call ??6std@@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@0@AAV10@PBD@Z ; std::operator<<
add esp, 8
mov ecx, eax
call ??6?$basic_ostream@DU?$char_traits@D@std@@@std@@QAEAAV01@P6AAAV01@AAV01@@Z@Z ; std::basic_ostream<char,std::char_traits<char> >::operator<<
; Line 14
mov ecx, DWORD PTR _this$[ebp]
call ?ClearALL@Parent@@QAEXXZ ; Parent::ClearALL
; Line 16
mov esp, ebp
pop ebp
ret 0
??1Parent@@QAE@XZ ENDP ; Parent::~Parent
_TEXT ENDS
首先看到的是改寫對象內存(現在沒有Parent和Child之分)中vptr(偏移 0~4)的指向-->>Parent::Vftable(一個地址,模型為一個表,表中存放的是其他函數的地址)
DWORD PTR [eax], OFFSET FLAT:??_7Parent@@6B@ ; Parent::`vftable'
然后下面幾行是打印字符串的,不看它哈,跳到:
mov ecx, DWORD PTR _this$[ebp]
call ?ClearALL@Parent@@QAEXXZ ; Parent::ClearALL
這個是當前對象內存的起始地址保存到ecx,然后調用Parent的成員函數::ClearAll..(這也是書上所說的成員函數的一個參數隱含為this指針,但它并不是push進去的哦~~這里到ecx中轉),現在來看ClearAll的匯編代碼:

Code
?ClearALL@Parent@@QAEXXZ PROC NEAR ; Parent::ClearALL, COMDAT
; File test.cpp
; Line 19
push ebp
mov ebp, esp
push ecx
mov DWORD PTR _this$[ebp], ecx
; Line 20
push OFFSET FLAT:?endl@std@@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@1@AAV21@@Z ; std::endl
push OFFSET FLAT:??_C@_0BA@OHDP@ClearALL?5?5?$HO?$HO?$HO?$HO?$HO?$AA@ ; `string'
push OFFSET FLAT:?cout@std@@3V?$basic_ostream@DU?$char_traits@D@std@@@1@A ; std::cout
call ??6std@@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@0@AAV10@PBD@Z ; std::operator<<
add esp, 8
mov ecx, eax
call ??6?$basic_ostream@DU?$char_traits@D@std@@@std@@QAEAAV01@P6AAAV01@AAV01@@Z@Z ; std::basic_ostream<char,std::char_traits<char> >::operator<<
; Line 21
mov eax, DWORD PTR _this$[ebp]
mov edx, DWORD PTR [eax]
mov ecx, DWORD PTR _this$[ebp]
call DWORD PTR [edx]
; Line 23
mov esp, ebp
pop ebp
ret 0
?ClearALL@Parent@@QAEXXZ ENDP ; Parent::ClearALL
在C++源文件中能看到ClearAll函數調用了ThePure,匯編代碼就是上面代碼中的:
mov eax, DWORD PTR _this$[ebp]
mov edx, DWORD PTR [eax]
mov ecx, DWORD PTR _this$[ebp]
call DWORD PTR [edx]
上面是一個獲取虛函數表的功能,然后把this指針放入ecx,然后調用 call DWORD PTR[edx]
[edx] 極為[edx+0],也極為虛函數表中第一個函數,現在該回去看看Parent::vftable了:
CONST SEGMENT
??_7Parent@@6B@ DD FLAT:__purecall ; Parent::`vftable'
CONST ENDS
就這樣子的,表中只有一項(Parent類中只一個虛函數),而且后面寫了 FLAT:__purecall (這個我不懂,應該是指向了一個"空"函數吧)
所以在ClearALl函數中調用ThePure,其實是調用了一個不存在的函數....所以出錯了.....
總結1:在派生類中由于某種原因(比如調用析構函數)將內存中vptr指向的表更改指向了了基類的表,而基類中存在純虛函數,并且在基類的某些地方存在調用純虛函數,就出錯.
至于最初那個鏈接文章中的第一個例子就更好理解了,我們來看看構造函數的匯編代碼,先看Base的:
_TEXT SEGMENT
_this$ = -4
??0Parent@@QAE@XZ PROC NEAR ; Parent::Parent, COMDAT
; File test.cpp
; Line 8
push ebp
mov ebp, esp
push ecx
mov DWORD PTR _this$[ebp], ecx
mov eax, DWORD PTR _this$[ebp]
mov DWORD PTR [eax], OFFSET FLAT:??_7Parent@@6B@ ; Parent::`vftable'
mov eax, DWORD PTR _this$[ebp]
mov esp, ebp
pop ebp
ret 0
??0Parent@@QAE@XZ ENDP
很簡單,這里僅僅是把Parent類的vftable地址分配到vptr(Parent對象內存起始的0~4偏移位置).
Child類的構造函數我就不貼了,其實主要一點先調用Parent類構造函數(如上,指定Parent類的vftable),然后就是如Parent一樣,把自己(Child)的vftable地址指定給vptr.
那么很明顯了,在Parent類中調用成員函數,然后在成員函數調用虛函數,根據當前vftable(Parent的)尋找出來的則肯定是純虛函數.....如果等Child構造好之后,Child會改寫虛函數表中的地址(哪個函數被改寫就改寫哪個),那么你調用Pure函數則不會出錯,因為其實調用的是Child的改寫版本,這是一個真實存在的函數.
--------------------------------------------------------------------------
在我這里沒有深究一些C++對象模型的一些其他更多問題,這里更多的是一個簡化,只為方便/簡單的窺視Pure function called.
posted on 2008-11-08 00:20
duzhongwei 閱讀(1570)
評論(0) 編輯 收藏 引用