2011-08-23 84 views
1

內存泄漏所以我覺得這是一種很難的問題的正確要求,但這裏是我最好的拍攝。與聲音合成

我正在寫obj-c中的iPhone應用程序,但它涉及聲音合成,並且我在覈心音頻中使用了聲音合成程序編寫了我認爲是C(或者C++,我問了我認識的人誰使用C++,但他不認識它,但教程告訴我要將文件名更改爲.mm for C++)。出現的問題是我有大量內存泄漏,很可能是因爲我不知道如何正確調用內容。

當曾經被使用的代碼的這一部分,我得到一噸的錯誤讀取這樣的: 2011-08-23 10:18:08.769 myProgram [451:5e03] * __NSAutoreleaseNoPool():對象_NSCallStackArray類的0x171e90自動釋放,沒有池到位 - 只是泄漏

使用工具,我已經找到泄漏發生的地方,並且已經發表了一條評論來表示這一點。

這裏是所有泄漏拿出功能:

static OSStatus renderInput(void *inRefCon, AudioUnitRenderActionFlags *ioActionFlags, const AudioTimeStamp *inTimeStamp, UInt32 inBusNumber, UInt32 inNumberFrames, AudioBufferList *ioData) 
{ 
// Get a reference to the object that was passed with the callback 
// In this case, the AudioController passed itself so 
// that you can access its data. 
AudioController *THIS = (AudioController*)inRefCon; 

// Get a pointer to the dataBuffer of the AudioBufferList 
AudioSampleType *outA = (AudioSampleType *)ioData->mBuffers[0].mData; 

// Calculations to produce a 600 Hz sinewave 
// A constant frequency value, you can pass in a reference vary this. 

float sinSignal; 

for (UInt32 i = 0; i < inNumberFrames; ++i) {  
    outA[i] = 0; 
} 


THIS->theBall = [[THIS->navDelegate ballsG] objectAtIndex:0]; 


THIS->amtPlaying = [[THIS->theBall playingLines] count]; 

//NSLog(@"%i", THIS->amtPlaying); 


for (int i = 0; i < THIS->amtPlaying; i++) 
{ 
    // Loop through the callback buffer, generating samples 
    for (UInt32 i = 0; i < inNumberFrames; ++i) {  

     // calculate the next sample 
     sinSignal = sin( [[[THIS->theBall playingLines] objectAtIndex:i] theta] ); //leak here 

     sinSignal *= [[[THIS->theBall playingLines] objectAtIndex:i] volume] ; 

     sinSignal /= THIS->amtPlaying; 

     // Put the sample into the buffer 
     // Scale the -1 to 1 values float to 
     // -32767 to 32767 and then cast to an integer 
     outA[i] += (SInt16)(sinSignal * 32767.0f); 
     // calculate the phase for the next sample 

     [[[THIS->theBall playingLines] objectAtIndex:i] increaseTheta: [[[THIS->theBall playingLines] objectAtIndex:i] incrementer]]; 
    } 

} 

return noErr; 
} 

如果我可以提供更多的信息,以幫助你理解我的問題,請讓我知道在評論。謝謝!

+0

如果你正在嘗試做你處理,你需要在函數的開頭添加一個'NSAutoreleasePool *池= [[NSAutoreleasePool的alloc]初始化]'然後'[池排水]'在年底由於背景你正在使用可能的自動釋放對象。 ('ballsG','objectAtIndex:','playingLines') – Joe

+0

這擺脫了其他的泄漏,現在的儀器顯示,我泄露NSAutoReleasePool。 – turbo

+0

你加入'[池排水]'權之前'返回諾爾;'?另外,當你啓動一個新線程時,無論哪種方法被調用,'NSAutoreleasePool'應該是在那裏創建的第一個東西,那麼最後完成的事情應該是用同樣的方法'排除'它。 – Joe

回答

2

你不應該使用在音頻任何objc代碼渲染線程。 Objc代碼可以鎖定,通常需要一些內存分配/釋放。這些應該也不會出現在音頻線程中,因爲它是高優先級的線程。 core-audio-api電子郵件列表中有大量討論資源。你可以嘗試做objc工作,但這是一場噩夢。使用c/C++也可以在沒有這些問題的情況下工作。

+0

對不起,這樣一個遲到的迴應,但該項目是第二天到期,所以我幾乎沒有觸及它,因爲。我對c/C++瞭解不多,所以我不知道它會爲我做多少好事。感謝您向我指出這一點,我會在將來或當我回到項目時記住它。 – turbo