Iphone 在Struct-calloc中使用指向AudioUnitSampleType的指针时内存泄漏

Iphone 在Struct-calloc中使用指向AudioUnitSampleType的指针时内存泄漏,iphone,pointers,memory-leaks,struct,audiounit,Iphone,Pointers,Memory Leaks,Struct,Audiounit,我正在为iphone编写一个音频应用程序,我需要使用一些C代码来处理音频文件。简言之,我有一个内存泄漏,导致应用程序在加载了这么多文件后崩溃。这个问题与我创建的一个在读入时保存音频文件的结构有关。结构的创建如下所示 类型定义结构{ UInt32 frameCount; // the total number of frames in the audio data UInt32 sampleNumber;

我正在为iphone编写一个音频应用程序,我需要使用一些C代码来处理音频文件。简言之,我有一个内存泄漏,导致应用程序在加载了这么多文件后崩溃。这个问题与我创建的一个在读入时保存音频文件的结构有关。结构的创建如下所示

类型定义结构{

    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];
- (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[phrase1Index]和phraseSynthStructArray[phrase2Index]的两个文件连接起来,并将组合文件放入phraseSynthStructArray[synthPhraseIndex]中,如下所示:

   soundStruct                  phraseSynthStructArray[3];
- (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;
}
这一切都很好,结果文件被连接并可以使用.audioDataLeft=AudioUnitSampleType*calloctotalFramesInFile,AudioUnitSampleType的大小;然后,下次调用该方法时,每次都会泄漏此内存,最终导致应用程序崩溃。我需要在此处分配内存的原因是,必须调整内存大小以容纳长度随大小而变化的连接文件输入文件的名称

在调用方法后,我无法在操作后释放内存,因为它在其他地方需要,我在上面的joinPhrases方法中尝试过释放内存,但这似乎不起作用。我也尝试过使用realloc释放/重新分配内存,方法是将指针传递到以前分配的内存,但这会导致崩溃状态正在执行错误的访问

我不是一个经验丰富的C程序员,无法找出我在这里做错了什么导致了泄漏。我希望能得到一些建议来帮助我追踪这个问题,因为我已经在这几天毫无乐趣地敲打我的头了。我读到在结构中使用指针是个坏主意,这可能是我问题的根源吗

提前感谢,, K.

也许这有助于:

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

并确保phraseSynthStructArray[synthPhraseIndex]中没有垃圾

您是否一直在使用泄漏工具?谢谢您的评论max。我已经尝试过了,但它没有崩溃,这是一个好迹象!不过,我在另一种方法中也做了类似的事情,将短语SynthStructArray传入如下:-BOOL readAudioFilesIntoMemory:SoundStructPtrArray并像以前一样分配内存:inaray[audioFile].audioDataLeft=AudioUnitSampleType*calloc totalFramesInFile,AudioUnitSampleType的大小;但是,如果我尝试在这里使用您的建议,它会崩溃,并声明malloc:**对象0xb716000的错误:未分配被释放的指针。知道为什么会发生这种情况吗?是的,我有一个想法。我写道:“确保短语中没有垃圾。”ynthStructArray[synthPhraseIndex]’。这意味着,当您声明指针时,即使它位于结构中,它通常也包含垃圾,而垃圾不会指向任何地方。这就是为什么您必须使用NULL初始化指针,以确保它们不会指向任何地方。例如,soundStruct s;s.audioDataLeft=NULL;s.audioDataRight=NULL;我已经尝试过了,但我仍然拥有内存ory问题。正如你上面所说的,我应该将s.audioDataLeft=NULL。这是否意味着在这样做的过程中,我确保如果old_ptr freeold_ptr;从未被调用,因此没有内存清理。对于这些简单的问题,我深表歉意,但我觉得在这个阶段我做了一些根本错误的事情。你还有其他建议可以帮助我吗请让我追踪这个问题。