我編碼的音頻應用程序的地方,我需要使用一些C代碼來處理音頻文件的iphone。總之,我有一個內存泄漏導致應用程序在加載了這麼多文件後崩潰。這個問題與我創建的Struct有關,它在讀入時保存音頻文件.Struct創建如下;內存泄漏 - 釋放calloc
typedef結構{
UInt32 frameCount; // the total number of frames in the audio data
UInt32 sampleNumber; // the next audio sample to play
BOOL isStereo; // set to true if there is data audioDataRight member
AudioUnitSampleType *audioDataLeft; // complete left channel of audio data read from file
AudioUnitSampleType *audioDataRight; // complete right channel of audio data read file
} soundStruct, *soundStructPtr;
結構體,然後在這樣的標題初始化;
soundStruct phraseSynthStructArray[3];
然後我試圖加入已經被讀入phraseSynthStructArray[phrase1Index]
和phraseSynthStructArray[phrase2Index]
並把合併後的文件爲phraseSynthStructArray[synthPhraseIndex]
這樣兩個文件;
- (BOOL) joinPhrases:(UInt32)phrase1Index phrase2Index:(UInt32)phrase2Index synthPhraseIndex:(UInt32)synthPhraseIndex{
// get the combined frame count
UInt64 totalFramesInFile = inArray[phrase1Index].frameCount + inArray[phrase2Index].frameCount;
//now resize the synthPhrase slot buffer to be the same size as both files combined
// phraseOut is used to hold the combined data prior to it being passed into the soundStructArray slot
free(phraseSynthStructArray[synthPhraseIndex].audioDataLeft);
phraseSynthStructArray[synthPhraseIndex].audioDataLeft = NULL;
phraseSynthStructArray[synthPhraseIndex].frameCount = 0;
phraseSynthStructArray[synthPhraseIndex].frameCount = totalFramesInFile;
phraseSynthStructArray[synthPhraseIndex].audioDataLeft = (AudioUnitSampleType *) calloc(totalFramesInFile, sizeof (AudioUnitSampleType));
for (UInt32 frameNumber = 0; frameNumber < inArray[phrase1Index].frameCount; ++frameNumber) {
phraseSynthStructArray[synthPhraseIndex].audioDataLeft[frameNumber] = phraseSynthStructArray[phrase1Index].audioDataLeft[frameNumber];
}
UInt32 sampleNumber=0;
for (UInt32 frameNumber = phraseSynthStructArray[phrase1Index].frameCount; frameNumber < totalFramesInFile; ++frameNumber) {
phraseSynthStructArray[synthPhraseIndex].audioDataLeft[frameNumber] = phraseSynthStructArray[phrase2Index].audioDataLeft[sampleNumber];
sampleNumber++;
}
return YES;
}
這一切都正常工作,生成的文件被連接並可以使用。我正在使用的是當我在這裏分配內存時,phraseSynthStructArray[synthPhraseIndex].audioDataLeft = (AudioUnitSampleType *) calloc(totalFramesInFile, sizeof (AudioUnitSampleType));
然後下次調用該方法時,此內存每次都會泄漏並最終導致應用程序崩潰。我需要在這裏分配內存的原因是因爲內存已被調整,以適應加入文件取決於輸入文件的大小而變化的長度。
在調用該方法後,我無法在其他位置調用該內存後釋放該內存,並試圖在釋放它之前(在上面的joinPhrases方法中),但這似乎不起作用。我一直在使用的realloc釋放/通過傳遞指針到以前分配的內存重新分配內存也試過,但這個casues崩潰,說明EXEC_BAD_ACCESS。
我不是一個經驗豐富的C程序員並不能找出我在做什麼錯在這裏造成泄漏。我希望有一些建議能夠幫助我追蹤這個問題,因爲我一直對這個問題一直耿耿於懷,連續幾天都沒有歡樂。我已經讀過這是一個不錯的主意,在構造指針中,這可能是我的問題的根源嗎?
由於提前, K.
你一直使用泄漏的工具嗎? – Max 2011-02-10 11:09:39