2016-05-26 38 views
0

我在我的Objective-C++類(MyObjectiveCppClass)以下-dealloc實現:調試__weak變量的不正確使用客觀的C++類

- (void)dealloc { 
    if (_my_iVar) { 
    [_my_iVar doSomeSlowishCleanUp]; 
    } 
} 

每當這個-dealloc方法被稱爲我得到像一堆警告:

objc[1254]: __weak variable at 0x1662a38c8 holds 0x19c70f408 instead of 0x160abe000. This is probably incorrect use of objc_storeWeak() and objc_loadWeak(). Break on objc_weak_error to debug. 

當我在objc_weak_error打破堆棧看起來是這樣的:

#0 0x0000000182395330 in objc_weak_error() 
#1 0x00000001823959d0 in weak_clear_no_lock() 
#2 0x000000018239f1e8 in objc_object::clearDeallocating_slow()() 
#3 0x000000018238e074 in objc_destructInstance() 
#4 0x0000000182d25fac in -[NSObject(NSObject) __dealloc_zombie]() 
----> #5 0x00000001000a6c30 in -[MyObjectiveCppClass .cxx_destruct] 
#6 0x0000000182382b54 in object_cxxDestructFromClass(objc_object*, objc_class*)() 
#7 0x000000018238e040 in objc_destructInstance() 
#8 0x0000000182d25fac in -[NSObject(NSObject) __dealloc_zombie]() 
----> #9 0x00000001000a691c in -[MyObjectiveCppClass dealloc] 
#10 0x0000000100611bc4 in -[SomeViewB .cxx_destruct] 
#11 0x0000000182382b54 in object_cxxDestructFromClass(objc_object*, objc_class*)() 
#12 0x000000018238e040 in objc_destructInstance() 
#13 0x0000000182d25fac in -[NSObject(NSObject) __dealloc_zombie]() 
#14 0x0000000188233a90 in -[UIResponder dealloc]() 
#15 0x0000000187e78b08 in -[UIView dealloc]() 
#16 0x0000000187f60700 in -[UIScrollView dealloc]() 
#17 0x0000000182c049b4 in -[__NSArrayM dealloc]() 
#18 0x00000001006495cc in -[SomeViewA .cxx_destruct] 

有沒有人有任何提示如何解決這些警告?

回答

0

事實證明,_my_iVar對象有自己的__unsafe_unretain引用,並將其傳遞給其中一個將其分配給weak局部變量的子對象。

+0

那麼解決方案是什麼?我遇到了類似的問題,但有第三方庫。 –

+0

你認爲解決方案是什麼?! –

1

可能是它可以幫助你,請按照下列步驟:

  1. 從您的課程中移除ARC(-fobjc弧)。要做到這一點,轉到項目設置 - >構建階段 - >編譯源。現在選擇你的班級並刪除-fobjc-arc。 現在構建它,如果您有任何autorelease,請將其從.m文件中的聲明中移除。假設你有:

    UILabel *label = [[[UILabel alloc] initWithFrame:rect] autorelease]; 
    

請做這樣:

UILabel *label = [[UILabel alloc] initWithFrame:rect]; 

不過你可能有一個問題,如:

Cannot synthesize weak property in file using manual reference counting. 

不要擔心的問題。

如果您沒有ARC,請轉到下一步。

  1. 刪除ARC後,再次轉到項目設置。轉到構建設置並搜索「手動保留釋放中的弱引用」。當前值爲NO。讓它成爲。

現在清潔,建立和運行。