c++ - 使用 STK 和 FMVoices 类的内存访问错误

标签 c++ pointers access-violation

我正在尝试使用 Stanford 的 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 乐器(如单簧管甚至 FileLoop 类)编写回调的方式,但 FMVoices 有一些奇怪的地方。该对象作为指向 void 的指针传递给 openStream(处理平台特定的实时输出)。当系统的音频缓冲区已满时自动调用回调。下面显示了实现此功能并且确实适用于其他仪器的代码片段:

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 的指针。

最佳答案

问题是您正在获取指针的地址,并将其传递给 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)

最快的解决方案是去掉 &在那一行。


现在对 C++ 进行一些评论,并对您的代码进行更多修复。该代码看起来像是 C 和 Java 的混合体,并且存在很多陷阱,其中之一导致了您的问题。

  • 不需要动态分配FMVoices .像处理 RtAudio dac 一样使用堆栈.
    • 无需担心指针,delete正在处理您分配的内存
    • 因此没有内存泄漏。
    • 只写FMVoices instrument_FM;
  • 没有必要做 try/catch在大多数情况下用于清理,因为 C++ 具有在作用域末尾触发并传播错误的析构函数。
    • 如果只用栈,就不用担心delete并进行清理操作
  • 永远不要使用 goto在 C++ 中,它真的不需要。 (不像在 C 中,它可以用于清理)。
    • 有析构函数和RAII为此。
  • 使用粒度更细的 C++ 转换,例如 static_cast<>reinterpret_cast<> , 而不是 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();
    }
}

关于c++ - 使用 STK 和 FMVoices 类的内存访问错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20559530/

相关文章:

使用 *param 和 &param 调用 C 函数

c - 分配新节点后根节点的值(空值)没有改变

c++ - 在 mac 中创建 dylib 在 python 中导入

c++ - 如何知道用户是否完全登录?

c - 将 int 相除并存储回原始变量

c++ - 什么可以使不更改地址的 Win32 C++ 指针突然指向无效内存?

vb.net - 访问类库中的 mustoverride 属性时出现 AccessViolationException

c++ - std::string 未正确终止

c++ - 定义不带终止符的 C++ 字符数组

c++-cli - 使用 pin_ptr 时出现访问冲突?