开发者

Memory Leak when using Pointer to AudioUnitSampleType in Struct - calloc

I am coding an audio app for the iphone where I need to use some C code to deal with the audio files. In short, I have a memory leak that is causing the app to crash after so many files have been loaded. The problem is related to a Struct that I create that holds the audio f开发者_开发知识库iles when read in. The Struct is created as follows;

typedef struct {

    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;

The Struct is then Initialized in the header like this;

   soundStruct                  phraseSynthStructArray[3];

I then attempt to join two files that have been read into phraseSynthStructArray[phrase1Index] and phraseSynthStructArray[phrase2Index] and put the combined file into phraseSynthStructArray[synthPhraseIndex] like this;

- (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;
}

This all works fine and the resulting file is joined and can be used. The isuue I am having is when I allocate the memory here, phraseSynthStructArray[synthPhraseIndex].audioDataLeft = (AudioUnitSampleType *) calloc(totalFramesInFile, sizeof (AudioUnitSampleType)); then next time the method is called, this memory leaks each time and eventually crashes the app. The reason I need to allocate the memory here is because the memory has to be resized to accomodate the joined file which varies in length depending on the size of the input files.

I cannot free the memory after the operation as its needed elsewhere after the method has been called and I have tried to free it before (in joinPhrases method above), but this does not seem to work. I have also tried using realloc to free/reallocate the memory by passing the pointer to the previously allocated memory but this casues a crash stating EXEC_BAD_ACCESS.

I am not a seasoned C programmer and Cannot figure out what I am doing wrong here to cause the leak. I would appreciate some advice to help me track down this issue as I have been banging my head against this for days with no joy. I have read thats its a bad idea to have Pointers in Structs, could this be the root of my problem?

Thanks in advance, K.


Maybe this helps:

- (BOOL) joinPhrases:(UInt32)phrase1Index phrase2Index:(UInt32)phrase2Index synthPhraseIndex:(UInt32)synthPhraseIndex{

    // get the combined frame count
    UInt64 totalFramesInFile = inArray[phrase1Index].frameCount + inArray[phrase2Index].frameCount;

. . .

    void* old_ptr = phraseSynthStructArray[synthPhraseIndex].audioDataLeft;
    phraseSynthStructArray[synthPhraseIndex].audioDataLeft = (AudioUnitSampleType *) calloc(totalFramesInFile, sizeof (AudioUnitSampleType));
    if( old_ptr ) free(old_ptr);

. . .


    return YES;
}

And make sure that there is no garbage in phraseSynthStructArray[synthPhraseIndex]

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜