objective-c - 声音合成的内存泄漏

标签 objective-c c ios xcode core-audio

所以我觉得这是一个很难正确提出的问题,但这是我最好的选择。

我正在用 obj-c 编写一个 iPhone 应用程序,但它涉及声音合成,我在核心音频中使用的教程使用了声音合成,我认为它是用 C(或者可能是 C++,我问过一个我认识的人使用的C++,他不认识它,但教程告诉我将文件名更改为 C++ 的 .mm)。出现的问题是我有巨大的内存泄漏,很可能是因为我不知道如何正确地调用东西。

每当使用这部分代码时,我都会收到大量如下所示的错误: 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;
}

如果我可以提供更多信息来帮助您理解我的问题,请在评论中告诉我。谢谢!

最佳答案

您不应该在音频渲染线程中使用任何 objc 代码。 Objc 代码可以锁定并且通常需要一些内存分配/释放。这些也不应该发生在音频线程中,因为它是一个高优先级线程。 core-audio-api 电子邮件列表中有大量讨论资源。您可以尝试让 objc 工作,但这是一场噩梦。如果没有这些问题,使用 c/c++ 也能正常工作。

关于objective-c - 声音合成的内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7163161/

相关文章:

ios - 如何从 NSMutableAttributedString 中删除文本

iphone - 如何在Objective C代码中使用Class对象

objective-c - postPath AFHTTPClient 中的 JSON 响应

objective-c - 为什么 objc_duplicateClass 标记为 "Do not call this function yourself"

html - 浏览器按原样显示由 C 程序写入套接字的 HTML 代码

ios - AFNetworking 预期读取的字节为 -1

ios - Swift:CollectionView 空间

objective-c - 使用 Xcode 的文本替换服务 - 不替换选定的文本

c - 使用指针表示法初始化数组

c - 在这种情况下我真的需要互斥锁吗?