【问题标题】:Memory Access error using STK and the FMVoices class使用 STK 和 FMVoices 类的内存访问错误
【发布时间】:2013-12-13 05:12:04
【问题描述】:

我正在尝试使用斯坦福大学的 STK 进行一些实时波表合成。我正在使用 FMVoices 乐器类https://ccrma.stanford.edu/software/stk/classstk_1_1FMVoices.html 并尝试在下面定义的回调例程中使用它。

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 乐器(如 Clarinet 甚至 FileLoop 类)编写回调的方式,但 FMVoices 有一些时髦。该对象作为指向 void 的指针传递给 openStream(处理特定于平台的实时输出)。当系统的音频缓冲区已满时,会自动调用回调。一个代码 sn-p 实现了这一点,并适用于其他仪器如下所示:

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 的大小似乎没有影响。在我看来,这些类型的错误通常来自引用指向 void 的指针。

【问题讨论】:

    标签: c++ pointers access-violation stk


    【解决方案1】:

    问题是你正在获取一个指针的地址,并将它传递给openStream

    // 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)
    

    最快的解决方案是删除该行中的&amp;


    现在有一些关于 C++ 的 cmets,还有一些对您的代码的修复。该代码看起来像是 C 和 Java 的混合体,并且会导致很多陷阱,其中一个导致了您的问题。

    • 不需要动态分配FMVoices。就像对RtAudio dac 所做的那样使用堆栈。
      • 无需担心指针,deleteing 分配的内存
      • 因此没有内存泄漏。
      • 只要写FMVoices instrument_FM;
    • 在大多数情况下不需要执行try/catch 进行清理,因为 C++ 具有在作用域结束时触发并传播错误的析构函数。
      • 如果只使用堆栈,则无需担心delete 和清理操作
    • 永远不要在 C++ 中使用goto,它真的不需要。 (不像在 C 中,它可以用于清理)。
      • 有析构函数和RAII
    • 使用更细粒度的 C++ 强制转换,例如 static_cast&lt;&gt;reinterpret_cast&lt;&gt;,而不是 C 风格的强制转换

    这是修改后的代码:

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

    【讨论】:

      猜你喜欢
      • 2012-06-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-07
      • 1970-01-01
      相关资源
      最近更新 更多