2012-04-12 65 views
1

在我的代碼我正在與音頻緩衝區,當我有一個回調函數,被稱爲每秒多次。此回調位於處理音頻的類中,而不在應用程序的主類中。autoreleased池說明

一開始我是讓這個警告是回調期間日誌多次:

Object 0x93cd5e0 of class __NSCFNumber autoreleased with no pool in place - just leaking - break on objc_autoreleaseNoPool() 

然後有人告訴我,把這個線在回調FUNC:

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 

那麼這錯誤消失。 但我不能理解這是如何可能的,我在1秒內分配池很多次 - 也許我有一個內存問題。

[pool drain]; 

,所以我有這樣的:

OSStatus status; 
    status = AudioUnitRender(audioUnit, 
          ioActionFlags, 
          inTimeStamp, 
          inBusNumber, 
          inNumberFrames, 
          &bufferList); 


    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; // that line added 

    //run on evert sample 
    int16_t *q = (int16_t *)(&bufferList)->mBuffers[0].mData; 
    for(int i=0; i < inNumberFrames; i++) 
    { 
    static int stateMachineSelector=1; 
    static int sampelsCounter=0; 

    // CODE TO HANDLE THE SAMPLES ... 
    } 
    [pool drain]; // issue here  

正是我在這裏做什麼 我比我必須把這個在年底看到? 這是爲什麼? 從內存方面來說可以嗎?

非常感謝。

回答

0

當啓動一個自動釋放池[[NSAutoreleasePool alloc] init]所有進一步的對象接收自動釋放或者與便利分配器創建(如NSArray *ary = [NSArray array];,或UIView *view = [[[UIView alloc] init] autorelease];是在游泳池

這樣:

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 
NSArray *ary = [NSArray array]; 
[pool release]; // this will also release and dealoc the *ary from memory 

如果您有一個回調運行NOT NOT主線程,你SHOULD做一個新的池。如果沒有,你的對象可能會韭菜涅:)。:)

如果使用autorelease對象處理大量數據,並且想要釋放內存,則創建一個池,處理並釋放該池。

+0

是的,我認爲我有另一個線程中的回調,它在另一個類中,並且正在運行,而我正在做其他的東西 - 所以我必須創建該池? ,另一件事,釋放它或排除它之間有什麼不同?,我看到有人說你必須排空它?非常感謝 。 – user1280535 2012-04-12 11:50:24

+0

「漏」是什麼意思?當你有一個'@ property'變量設置爲(retain)時,你可以通過調用'self.yourPropertyVar = nil; //因爲這會做[yourPropertyVar發佈]'。但不是'yourPropertyVar = nil; //(沒有自己!),會發生mem泄漏!' – 2012-04-12 13:39:16

+0

您還可以使用「新」語言級自動釋放池:'@autoreleasepool {..}'' @同步'塊).. – Francesco 2012-04-12 14:35:12