2015-08-24 49 views
2

我的二進制崩潰,並以下線產生異常看起來像這樣的Android例外不從共享庫抓

try { 
    std::ofstream out; 
    out.exceptions(std::ofstream::failbit | std::ofstream::badbit); 
    out.open("bad_path"); 
} catch(std::ios_base::failure &e) { 
    // skipped 
} 

出於某種原因的異常沒有被抓到,所以我加入

I DEBUG : pid: 1190, tid: 1367, name: Binder_1 >>> /system/bin/fingerprint_proxy <<< 
I DEBUG : signal 6 (SIGABRT), code -6 (SI_TKILL), fault addr -------- 
I DEBUG : Abort message: 'terminating with uncaught exception of type std::__1::ios_base::failure: ios_base::clear: unspecified iostream_category error' 

代碼多個catch塊

try { 
    std::ofstream out; 
    out.exceptions(std::ofstream::failbit | std::ofstream::badbit); 
    out.open("bad_path"); 
} catch(std::ios_base::failure &e) { 
    // skipped 
} catch(std::__1::ios_base::failure &e) { 
    // skipped 
} catch(std::exception &e) { 
    // skipped 
} catch(...) { 
    // caught 
} 

此時只有最後一個catch塊捕捉到的異常, ,並確定它不是我添加的繼承問題。

try { 
    throw std::ios_base::failure("miau"); 
} catch(std::exception &e) { 
    // caught 
} 

我Android.mk看起來像這樣

include $(CLEAR_VARS) 

LOCAL_CFLAGS := -Wall 
LOCAL_CPPFLAGS := -Wno-write-strings -Wall -std=c++11 -fexceptions 

LOCAL_SRC_FILES := test.cpp 

LOCAL_MODULE := test_app 
LOCAL_MODULE_TAGS := optional 

include external/libcxx/libcxx.mk 
include $(BUILD_EXECUTABLE) 

我得到的std ::從共享庫libC++命名空間和異常的實現。因此,其中包括由 「包括外部/ libcxx/libcxx.mk」。 所以我編譯libC++。所以staticlly。

include $(CLEAR_VARS) 

LOCAL_CFLAGS := -Wall 
LOCAL_CPPFLAGS := -Wno-write-strings -Wall -std=c++11 -fexceptions 

LOCAL_SRC_FILES := test.cpp 

LOCAL_MODULE := test_app 
LOCAL_MODULE_TAGS := optional 

LOCAL_SHARED_LIBRARIES := libc++ 

include external/libcxx/libcxx.mk 
include $(BUILD_EXECUTABLE) 

它的工作!

異常被std :: exception子句捕獲, 儘管它將二進制大小乘以8,所以這不是解決方案。

所以看起來像共享庫引發的異常沒有被捕獲。

任何人有想法爲什麼?

其主旨在於爲您提供方便 https://gist.github.com/amfern/2377e9a3cd1ccc0186ea

+0

它是['std :: exception'](http://en.cppreference.com/w/cpp/error/exception)不是'std :: exceptions' – NathanOliver

+0

錯字在問題中,對不起 – amfern

回答

0

添加到-frtti LOCAL_CPPFLAGS解決了這個問題。

由於異常在內部使用RTTI,因此不指定-frtti標誌將生成兩個表,一個來自libC++,一個來自我的二進制。 exceptions under the hood

注意:由於所有android本機庫都是在沒有RTTI的情況下編譯的,因此無法將它們包含在-frtti編譯的二進制/庫中。