2012-01-06 31 views
2

編輯:已解決。問題更加溫和 - 我有兩個函數在一行代碼中互相調用 - 使用lexical_cast其他一個崩潰。有趣的是,我只能通過噴灑很多cout的信息來發現這個問題,因爲在碰撞時沒有回溯,並且當調試線路成爲線路時,gdb出於任何原因顯示錯誤的lexical_cast是罪魁禍首(並且我沒有別看另一個,嘆氣)。謝謝您的幫助!在共享庫中提升異常


我使用的是gcc 4.1.2和boost 1.48。我有一個共享庫的模板函數內部下面的代碼:

try { 
    boost::lexical_cast<T>(s); 
} 
catch (...) { 
    std::cout << "Caught it" << std::endl; 
    throw; 
} 

轉換失敗,但異常不會被逮住(它確實繁殖和終止程序,但這種趕超條款沒有按趕不上)。 Tlongsstd::string等於"234a234"。 (我也試過包裝在#pragma GCC visibility push(default)包括提升包括在內,並且還嘗試在鏈接時添加-shared-libgcc標誌,並且沒有做任何事情。)

雖然它變得更好。在以下兩種情況下的例外情況被逮住:

try { 
    throw boost::bad_lexical_cast(); 
} 
catch (...) { 
    std::cout << "Caught it" << std::endl; 
    throw; 
} 

和令人驚訝這一個:

try { 
    boost::lexical_cast<T>(s); 
    throw boost::bad_lexical_cast(); 
} 
catch (...) { 
    std::cout << "Caught it" << std::endl; 
    throw; 
} 

上發生了什麼事情,更重要的任何想法如何解決這一問題?

+0

爲什麼抓不住的boost :: bad_lexical_cast&或std ::例外&呢?它不回答你的問題,但也許它會幫助編譯器。 – Joel 2012-01-06 19:44:44

+0

試過 - 沒有幫助:( – eddi 2012-01-06 19:48:47

回答

0

如果BOOST_NO_EXCEPTIONS被定義在某處,會發生這種情況。

+0

不是這樣,也許我有點不清楚 - 拋出boost :: bad_lexical_cast異常並且程序終止,但是這個異常不會被捕獲塊捕獲 – eddi 2012-01-06 19:58:51

+0

這是奇怪的。我看到你的catch子句重新拋出它,你有沒有在那裏設置斷點,或者你是否依靠'std :: cout'來查看你是否發現了這個異常? – 2012-01-06 20:58:55

+0

我試着設置一個斷點 - 它永遠不會到達那裏。事實上,當它死了,我甚至不能得到一個正常的回溯 - 它只是顯示'libgcc' smth,然後只是幾幀''' – eddi 2012-01-06 21:02:35

1

我無法重現我的機器上,但我使用的是不同的編譯器

gcc version 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.1.00) 

我用下面的測試:

#include <iostream> 
#include <boost/lexical_cast.hpp> 
#include <string> 
#include <exception> 

using namespace std; 

template<typename T> 
T printLexicalCast(const std::string& s){ 
    T t; 
    try { 
     t = boost::lexical_cast<T>(s); 
     cout << "cast is [" << t << "] from string [" << s << "]" << endl; 
    } 
    catch (const boost::bad_lexical_cast& e) { 
     std::cout << "Caught bad lexical cast with error " << e.what() << std::endl; 
    } 
    catch(...){ 
     std::cout << "Unknown exception caught!" << endl; 
    } 
    return t; 
} 


int main(int argc, char *argv[]) { 

    std::string badString("234a234"); 
    long l1 = printLexicalCast<long>(badString); //exception 


    std::string goodString("123456"); 
    long l2 = printLexicalCast<long>(goodString); 

    return 0; 
} 

我得到以下的輸出:

Caught bad lexical cast with error bad lexical cast: source type value could not be interpreted as target 
cast is [123456] from string [123456] 

如果我刪除bad_lexical_cast捕捉所有的作品。

Unknown exception caught! 
cast is [123456] from string [123456] 

也許這只是一個編譯器錯誤?我發現這對提升用戶列出

http://boost.2283326.n4.nabble.com/conversion-lexical-cast-doesn-t-throw-td2593967.html

+0

據我瞭解這是一個共享庫唯一的問題。 當我不使用任何庫時,我可以捕獲異常,但是一旦我將代碼放入庫中,它就會停止工作。有關共享庫中的例外的一些討論[這裏](http://gcc.gnu.org/wiki/Visibility)和[here](http://lists.boost.org/boost-users/2008/09/ 40268.php),但那些東西沒有幫助我。 – eddi 2012-01-06 20:59:53

+0

爲了完整性 - 我的實際代碼結構稍微複雜一些 - 例外情況出現在我正在使用的另一個庫使用的庫中。 – eddi 2012-01-06 21:07:47