首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用STK和FMVoices类访问内存错误

使用STK和FMVoices类访问内存错误
EN

Stack Overflow用户
提问于 2013-12-13 05:12:04
回答 1查看 118关注 0票数 0

我试着用斯坦福的STK做一些实时的可波合成。我正在使用FMVoices to类1FMVoices.html,并试图在下面定义的回调例程中使用它。

代码语言:javascript
复制
int tick( void *outputBuffer, void *inputBuffer, unsigned int nBufferFrames,
         double streamTime, RtAudioStreamStatus status, void *dataPointer )
{

  FMVoices *FM = (FMVoices *) dataPointer;
  register StkFloat *samples = (StkFloat *) outputBuffer;

  for ( unsigned int i=0; i<nBufferFrames; i++ )
    *samples++ = FM->tick();

  return 0;
}

我认为问题在于最后一个参数的类型。我得到一个运行时错误:"0xC0000005:执行位置0x000000001的访问冲突。“现在,这种回调应该是为其他STK乐器编写的,比如单簧管,甚至是FileLoop类,但是FMVoices有一些奇怪的地方。该对象被传递给openStream (它处理特定于平台的实时输出),作为一个指向void的指针。当系统的音频缓冲区已满时,会自动调用回调。实现此功能并为其他工具工作的代码片段如下所示:

代码语言:javascript
复制
int main()
{
 // Set the global sample rate before creating class instances.
  Stk::setSampleRate( 44100.0 );
  RtAudio dac;



  Instrmnt * instrument_FM;


  int nFrames = 10000;
  try {

   instrument_FM = new FMVoices;

  }
  catch ( StkError & ) {
  goto cleanup;
  }


instrument_FM->setFrequency(440.0);


// Figure out how many bytes in an StkFloat and setup the RtAudio stream.
RtAudio::StreamParameters parameters;
parameters.deviceId = dac.getDefaultOutputDevice();
parameters.nChannels = 1;
RtAudioFormat format = ( sizeof(StkFloat) == 8 ) ? RTAUDIO_FLOAT64 : RTAUDIO_FLOAT32;
unsigned int bufferFrames = RT_BUFFER_SIZE;
try {
  dac.openStream( &parameters, NULL, format, (unsigned int)Stk::sampleRate(), &bufferFrames, &tick, (void *)&instrument_FM);
  }
  catch ( RtError &error ) {
  error.printMessage();
  Sleep(1000);
goto cleanup;

}

nFrames的大小似乎没有影响。在我看来,这些类型的错误通常来自于引用指向空的指针。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-12-13 05:52:54

问题是,您正在获取指针的地址,并将其传递到openStream中。

代码语言:javascript
复制
// pointer to instrument
Instrmnt * instrument_FM;

// snip ...

// &instrument_FM is a pointer to a pointer! i.e. Instrmnt **
dac.openStream( &parameters, /* other params */, (void *)&instrument_FM)

最快的解决方案就是去掉该行中的&

现在有一些关于C++的注释,以及对您的代码的更多修正。代码看起来像是C和Java的混合体,并打开了许多陷阱,其中一个导致了您的问题。

  • 不需要动态分配FMVoices。使用堆栈就像使用RtAudio dac一样。
    • 不需要担心指针,也不必担心分配的内存的delete
    • 因此没有内存泄漏。
    • 只需写FMVoices instrument_FM;

  • 在大多数情况下,不需要执行try/catch来进行清理,因为C++具有在作用域结束时触发并传播错误的析构函数。
    • 如果您只使用堆栈,则不需要担心delete和清理操作。

  • 不要在goto中使用C++,它真的不需要。(与C语言不同,在C中,它可以用于清理)。
    • 有析构函数和雷伊

  • 使用更细粒度的C++强制转换,如static_cast<>reinterpret_cast<>,而不是C风格的。
    • 见本文的解释。

以下是修改后的代码:

代码语言:javascript
复制
int main()
{
    // Set the global sample rate before creating class instances.
    Stk::setSampleRate( 44100.0 );
    RtAudio dac;

    FMVoices instrument_FM;
    instrument_FM.setFrequency(440.0);    

    // Figure out how many bytes in an StkFloat and setup the RtAudio stream.
    RtAudio::StreamParameters parameters;
    parameters.deviceId = dac.getDefaultOutputDevice();
    parameters.nChannels = 1;
    RtAudioFormat format = ( sizeof(StkFloat) == 8 ) ? RTAUDIO_FLOAT64 : RTAUDIO_FLOAT32;
    unsigned int bufferFrames = RT_BUFFER_SIZE;

    // didn't get rid of this try since you want to print the error message.
    try {
        // note here i need the ampersand &, because instrument_FM is on the stack
        dac.openStream( &parameters, NULL, format, static_cast<unsigned int>(Stk::sampleRate()), &bufferFrames, &tick, reinterpret_cast<void*>(&instrument_FM));
    }
    catch ( RtError& error ) {
        error.printMessage();
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20559530

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档