2013-06-22 136 views
2

我正在使用boost 1.53.0,並且直到現在都沒有問題(並且使用了套接字,定時器,容器,算法,所有這些都沒有問題)。BOOST_THROW_EXCEPTION導致無限遞歸

我喜歡使用增強異常的想法,特別是因爲行號和不行。

然而,在我的(超級簡單)的代碼:通過CMake生成

#include <iostream> 
#include <fstream> 

#include <boost/scoped_ptr.hpp> 
#include <boost/exception/all.hpp> 

struct my_error: virtual boost::exception, virtual std::exception { }; 

int main(int argc, char* argv[]) 
{ 
    try 
    { 
    BOOST_THROW_EXCEPTION(my_error()); 
    } 
    catch(...) 
    { 
    std::cout <<"fail"; 
    } 
} 

項目(希望這不是擰起來)

cmake_minimum_required (VERSION 2.8) 
project(error_test) 
IF(WIN32) 
    set(Boost_USE_STATIC_LIBS ON) 
    set(Boost_USE_MULTITHREADED ON) 
    set(Boost_USE_STATIC_RUNTIME OFF) 
    set(Boost_NO_SYSTEM_PATHS FALSE) 
ENDIF() 

find_package(Boost COMPONENTS system date_time) 
include_directories(${Boost_INCLUDE_DIRS} 
    ) 



add_executable(${PROJECT_NAME} main.cpp) 

target_link_libraries(${PROJECT_NAME} 
${Boost_LIBRARIES}) 

不是引發的,BOOST_THROW_EXCEPTION進入一個無限遞歸!

編譯器甚至抓住這一點,說明編譯器警告

警告C4717:「助推:: exception_detail :: throw_exception_」:遞歸上的所有控制路徑,功能會導致運行時堆棧溢出。

,它只是不斷的打擊:

test.exe!boost::exception_detail::throw_exception_<my_error>(const my_error & x, const char * current_function, const char * file, int line) Line 84 + 0xd1 bytes C++ 

我使用Visual Studio 2010(贏64)。我內置升壓使用下面的命令,如果這能幫助:

.\b2 install --prefix=C:\devtools\boost_1_53_0 --toolset=msvc --build-type=complete --build-dir=C:\devtools\bin\boost_1_53_0 address-model=64 architecture=x86 

EDIT增加擴展的宏:

看起來像宏展開

::boost::exception_detail::throw_exception_(my_error(), __FUNCSIG__ ,"main.cpp",40); 

它擴展到

throw_exception_(E const & x, char const * current_function, char const * file, int line) 
{ 
    ::boost::exception_detail::throw_exception_(set_info(set_info(set_info(enable_error_info(x), throw_function(current_function)), throw_file(file)), throw_line(line)), __FUNCSIG__ ,"C:\\devtools\\boost_1_53_0\\boost/throw_exception.hpp",91); 

#line 92「C:\ devtools \ boost_1_53_0 \ boost/throw_exception.hpp」 }

+1

你能給一個完整的最小工作的例子,包括頭文件等? –

回答

3

這簡直太奇怪了。正如你可以很容易地檢查https://svn.boost.org/svn/boost/tags/release/Boost_1_53_0/boost/throw_exception.hpp boost :: exception :: throw_exception_根本不是遞歸的。

我可以看到發生這樣的事情的唯一方法是與邪惡的宏。請在每個包含指令之前和之後嘗試將它放在主文件中。

#if defined(throw_exception) || defined(throw_exception_) 
#error Somebody set us up the bomb 
#endif 
+0

boost :: throw_exception很好,但它不會添加__LINE__信息等。它的BOOST_THROW_EXCEPTION宏給我帶來了問題。無限循環在'exception_detail :: throw_exception_'中。它調用另一個BOOST_THROW_EXCEPTION。我將擴展宏並將其添加到我的問題中,以便我們可以清楚地看到問題。 – IdeaHat

+0

此外,我應該說,我確認沒有人「設置我們的炸彈」 – IdeaHat

+0

我會繼續前進,並給你答案的信貸,因爲「你打破了什麼」實際上已經死了。 – IdeaHat

0

OK,所以它看起來像某種原因

throw_exception_(E const & x, char const * current_function, char const * file, int line) 
    { 
     boost::throw_exception(
      set_info(
       set_info(
        set_info(
         enable_error_info(x), 
         throw_function(current_function)), 
        throw_file(file)), 
       throw_line(line))); 
    } 

改爲

throw_exception_(E const & x, char const * current_function, char const * file, int line) 
    { 
     BOOST_THROW_EXCEPTION(
      set_info(
       set_info(
        set_info(
         enable_error_info(x), 
         throw_function(current_function)), 
        throw_file(file)), 
       throw_line(line))); 
    } 
在我的代碼

...所以我必須打破我自己的提升版本。抱歉關於狂放的追逐!我投票關閉這個線程...