C++,一种"不可能"的行为

Tho*_*ini 5 c++ assembly

如果你已经编程了一段时间,那么你可能会注意到一些完全不可能的事情时不时发生,你确信没有可能的解释("这是一个编译器BUG !!").在你发现它是由什么造成的,虽然你像"oooohhh".

嗯,它恰巧发生在我身上:(

这里AuthDb 不是 NULL,而是一个有效的指针:

SingleResult sr(AuthDb, format("SELECT Id, Access, Flags, SessionKey, RealmSplitPreference FROM accounts WHERE Name = '%s'") % Escaped(account_name));
Run Code Online (Sandbox Code Playgroud)

在这里它神秘地变为NULL:

struct SingleResult : public BaseResult
{
    SingleResult(Database *db, const boost::format& query)  { _ExecuteQuery(db, query.str()); }
}
Run Code Online (Sandbox Code Playgroud)

请注意,这是下一个电话.两个屏幕截图可以解释得更好:http: //img187.imageshack.us/img187/5757/ss1zm.png
http://img513.imageshack.us/img513/5610/ss2b.png

编辑:AuthDb是一个全局变量.它本身一直指向正确的事物; 但是ptr Database*db的副本指向NULL.


ASM代码(遗憾的是我甚至不知道如何阅读它:/)

第一个截图

01214E06  mov         eax,dword ptr [ebp-328h] 
01214E0C  push        eax  
01214E0D  push        offset string "SELECT Id, Access, Flags, Sessio"... (13C6278h) 
01214E12  lea         ecx,[ebp-150h] 
01214E18  call        boost::basic_format<char,std::char_traits<char>,std::allocator<char> >::basic_format<char,std::char_traits<char>,std::allocator<char> > (11A3260h) 
01214E1D  mov         dword ptr [ebp-32Ch],eax 
01214E23  mov         ecx,dword ptr [ebp-32Ch] 
01214E29  mov         dword ptr [ebp-330h],ecx 
01214E2F  mov         byte ptr [ebp-4],2 
01214E33  mov         ecx,dword ptr [ebp-330h] 
01214E39  call        boost::basic_format<char,std::char_traits<char>,std::allocator<char> >::operator%<Snow::Escaped> (11A3E18h) 
01214E3E  push        eax  
01214E3F  mov         edx,dword ptr [__tls_index (144EC40h)] 
01214E45  mov         eax,dword ptr fs:[0000002Ch] 
01214E4B  mov         ecx,dword ptr [eax+edx*4] 
01214E4E  mov         edx,dword ptr [ecx+12A3Ch] 
01214E54  push        edx  
01214E55  lea         ecx,[sr] 
01214E58  call        Snow::SingleResult::SingleResult (11A27D4h) 
01214E5D  mov         byte ptr [ebp-4],4 // VS GREEN ARROW IS HERE
01214E61  lea         ecx,[ebp-150h] 
01214E67  call        boost::basic_format<char,std::char_traits<char>,std::allocator<char> >::~basic_format<char,std::char_traits<char>,std::allocator<char> > (11A1DBBh) 
01214E6C  mov         byte ptr [ebp-4],5 
01214E70  lea         ecx,[ebp-170h] 
01214E76  call        Snow::Escaped::~Escaped (11A42D2h) 
    const bool account_found = !sr.Error();
01214E7B  lea         ecx,[sr] 
01214E7E  call        Snow::BaseResult::Error (11A2964h) 
01214E83  movzx       eax,al 
01214E86  test        eax,eax 
01214E88  sete        cl   
01214E8B  mov         byte ptr [account_found],cl 

    if (!account_found) {
01214E8E  movzx       edx,byte ptr [account_found] 
01214E92  test        edx,edx 
01214E94  jne         AuthSession+1C0h (1214F10h) 
        client.Kill(format("%s: Attempted to login with non existant account `%s'") % client % account_name, true);
Run Code Online (Sandbox Code Playgroud)

第二个截图

011A8E7D  mov         dword ptr [ebp-10h],ecx 
011A8E80  mov         ecx,dword ptr [this] 
011A8E83  call        Snow::BaseResult::BaseResult (11A31D9h) 
011A8E88  mov         dword ptr [ebp-4],0 
011A8E8F  lea         eax,[ebp-30h] 
011A8E92  push        eax  
011A8E93  mov         ecx,dword ptr [query] 
011A8E96  call        boost::basic_format<char,std::char_traits<char>,std::allocator<char> >::str (11A1E01h) 
011A8E9B  mov         dword ptr [ebp-34h],eax 
011A8E9E  mov         ecx,dword ptr [ebp-34h] 
011A8EA1  mov         dword ptr [ebp-38h],ecx 
011A8EA4  mov         byte ptr [ebp-4],1 
011A8EA8  mov         edx,dword ptr [ebp-38h] 
011A8EAB  push        edx  
011A8EAC  mov         eax,dword ptr [db] 
011A8EAF  push        eax  
011A8EB0  mov         ecx,dword ptr [this] 
011A8EB3  call        Snow::SingleResult::_ExecuteQuery (124F380h) 
011A8EB8  mov         byte ptr [ebp-4],0 // VS GREEN ARROW HERE
011A8EBC  lea         ecx,[ebp-30h] 
011A8EBF  call        std::basic_string<char,std::char_traits<char>,std::allocator<char> >::~basic_string<char,std::char_traits<char>,std::allocator<char> > (11A2C02h) 
011A8EC4  mov         dword ptr [ebp-4],0FFFFFFFFh 
011A8ECB  mov         eax,dword ptr [this] 
011A8ECE  mov         ecx,dword ptr [ebp-0Ch] 
011A8ED1  mov         dword ptr fs:[0],ecx 
011A8ED8  pop         edi  
011A8ED9  add         esp,38h 
011A8EDC  cmp         ebp,esp 
011A8EDE  call        _RTC_CheckEsp (12B4450h) 
011A8EE3  mov         esp,ebp 
011A8EE5  pop         ebp  
011A8EE6  ret         8    
Run Code Online (Sandbox Code Playgroud)

更新 根据peterchen的建议,我ASSERT(AuthDb);在这里补充:

ASSERT(AuthDb);
SingleResult sr(AuthDb, format("SELECT Id, Access, Flags, SessionKey, RealmSplitPreference FROM accounts WHERE Name = '%s'") % Escaped(account_name));
Run Code Online (Sandbox Code Playgroud)

它失败了Oo然而调试器仍然坚持认为它不是NULL ..它没有被本地阴影

cout << AuthDb;即使调试器说它不是UPDATE2*也是0


发现了问题

Database *AuthDb = NULL, *GameDb = NULL; 在一个.cpp

extern thread Database *AuthDb, *GameDb; 在.h

该变量在标题中标记了线程(TLS - 线程本地存储),但在定义中没有标记TLS ...

无数小时浪费在这个超级愚蠢的错误上,没有任何警告或暗示或编译器中的任何我现在想要杀死的东西.:(哦,好吧,就像我说的每个不可能的行为有一个曾经知道的解决方案似乎很明显:)

感谢所有帮助过的人,我真的很绝望!

Jas*_*rff 9

AuthDB是一个线程局部变量吗?

也许调试器没有正确处理它.如果你在调用构造函数之前ASSERT(AuthDB)怎么办?

更新:如果它是线程本地的,它只是尚未在此线程中初始化.