所以我觉得这是一个很难正确提出的问题,但这是我最好的选择。
我正在用 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/