2011-08-23 82 views
1

内存泄漏所以我觉得这是一种很难的问题的正确要求,但这里是我最好的拍摄。与声音合成

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

当曾经被使用的代码的这一部分,我得到一吨的错误读取这样的: 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; 
} 

如果我可以提供更多的信息,以帮助你理解我的问题,请让我知道在评论。谢谢!

+0

如果你正在尝试做你处理,你需要在函数的开头添加一个'NSAutoreleasePool *池= [[NSAutoreleasePool的alloc]初始化]'然后'[池排水]'在年底由于背景你正在使用可能的自动释放对象。 ('ballsG','objectAtIndex:','playingLines') – Joe

+0

这摆脱了其他的泄漏,现在的仪器显示,我泄露NSAutoReleasePool。 – turbo

+0

你加入'[池排水]'权之前'返回诺尔;'?另外,当你启动一个新线程时,无论哪种方法被调用,'NSAutoreleasePool'应该是在那里创建的第一个东西,那么最后完成的事情应该是用同样的方法'排除'它。 – Joe

回答

2

你不应该使用在音频任何objc代码渲染线程。 Objc代码可以锁定,通常需要一些内存分配/释放。这些应该也不会出现在音频线程中,因为它是高优先级的线程。 core-audio-api电子邮件列表中有大量讨论资源。你可以尝试做objc工作,但这是一场噩梦。使用c/C++也可以在没有这些问题的情况下工作。

+0

对不起,这样一个迟到的回应,但该项目是第二天到期,所以我几乎没有触及它,因为。我对c/C++了解不多,所以我不知道它会为我做多少好事。感谢您向我指出这一点,我会在将来或当我回到项目时记住它。 – turbo