簡體   English   中英

指針和發布版本在Visual Studio中

[英]Pointers and release build in Visual Studio

使用Visual Studio 2008創建發行版本時,我遇到一個奇怪的問題。我想知道你們中的一個人是否可以幫助我了解發生了什么。

說明:我有一個類成員函數,該函數返回指向存儲在該類中的結構的指針:

const MyStruct * Myclass::getPointer() { 
    return mystruct_variable; // this is properly initialyzed 
}

值得指出的另一點是,此類/方法在dll中,我將其導出以在單獨的可執行項目中使用。 當我進行發行版本構建並嘗試使用上述方法時,運行會崩潰,具體取決於是否將getPointer()方法內聯(即放在類的頭文件中)或內聯(放在cpp文件中)。

用法是:

const MyStruct * cf = myclassObj.getPointer();
int n = cf->size_t;
std::cout<<n<<std::endl;

當MyClass :: getPointer()內聯到標題中時,程序集如下所示:

const MyStruct * cf = myclassObj.getPointer();
  012514A8  mov         esi,dword ptr [ebx+794h] 
int n =cf->size_t;
  012514AE  mov         eax,dword ptr [esi+20h] 
std::cout<<n<<std::endl;
  012514B1  mov         ecx,dword ptr [__imp_std::endl (1252038h)] 
  012514B7  push        ecx  
  012514B8  mov         ecx,dword ptr [__imp_std::cout (125203Ch)] 
  012514BE  push        eax  
  012514BF  call        dword ptr [__imp_std::basic_ostream<char,std::char_traits<char> >::operator<< (1252048h)] 
  012514C5  mov         ecx,eax 
  012514C7  call        dword ptr [__imp_std::basic_ostream<char,std::char_traits<char> >::operator<< (1252044h)] 

當未內聯getPointer()的類方法並將其放置在相應的cpp文件中時,相同的代碼給出:

const MyStruct * cf = myclassObj.getPointer();
  00DA14A8  mov         ecx,ebx 
  00DA14AA  call        dword ptr [__imp_MyClass::getPointer(0DA2104h)] 
int n =cf->size_t;
std::cout<<n<<std::endl;
  00DA14B0  mov         ecx,dword ptr [__imp_std::endl (0DA2038h)] 
  00DA14B6  mov         esi,eax 
  00DA14B8  mov         eax,dword ptr [esi+20h] 
  00DA14BB  push        ecx  
  00DA14BC  mov         ecx,dword ptr [__imp_std::cout (0DA203Ch)] 
  00DA14C2  push        eax  
  00DA14C3  call        dword ptr [__imp_std::basic_ostream<char,std::char_traits<char> >::operator<< (0DA2048h)] 
  00DA14C9  mov         ecx,eax 
 00DA14CB  call        dword ptr [__imp_std::basic_ostream<char,std::char_traits<char> >::operator<< (0DA2044h)] 

有什么想法為什么這兩個案例具有不同的集合集? 難道我做錯了什么? 謝謝!

如果鏈接到C ++ DLL,則必須確保所有編譯器標志都完全相同 否則,結構,虛擬表等的大小可能會有所不同,並且代碼將因對內存的無效訪問而失敗。 內聯當然可以解決此問題,因為代碼位於exe文件中,而不是dll中,因此可以使用正確的標志進行編譯。

簡而言之-對於發布版本,請使用發布DLL,對於調試版本,請使用調試DLL。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM