DirectSound的例子程序CaptureSound(.NET version)的一个Bug
2007-09-13 18:23
411 查看
最近笔者在.NET平台上学着使用DirectSound来做一个简单的录音和WAV文件合并的功能, 当然DirectX自带的CaptureSound例子提供了一切录音的源代码,不过我发现其中有个Bug,导致Release版本的程序无辜退出。
DirectX SDK的版本: August 2007.
代码(位于文件MainForm.cs):
void InitNotifications()
{
//-----------------------------------------------------------------------------
// Name: InitNotifications()
// Desc: Inits the notifications on the capture buffer which are handled
// in the notify thread.
//-----------------------------------------------------------------------------
if (null == applicationBuffer)
throw new NullReferenceException();
// Create a thread to monitor the notify events
if (null == NotifyThread)
{
NotifyThread = new Thread(new ThreadStart(WaitThread));
Capturing = true;
NotifyThread.Start();
// Create a notification event, for when the sound stops playing
NotificationEvent = new AutoResetEvent(false);
}
// Setup the notification positions
for (int i = 0; i < NumberRecordNotifications; i++)
{
PositionNotify[i].Offset = (NotifySize * i) + NotifySize - 1;
PositionNotify[i].EventNotifyHandle = NotificationEvent.Handle;
}
applicationNotify = new Notify(applicationBuffer);
// Tell DirectSound when to notify the app. The notification will come in the from
// of signaled events that are handled in the notify thread.
applicationNotify.SetNotificationPositions(PositionNotify, NumberRecordNotifications);
}
private void WaitThread()
{
while(Capturing)
{
//Sit here and wait for a message to arrive
NotificationEvent.WaitOne(Timeout.Infinite, true);
RecordCapturedData();
}
}
注意,在WaitThread被第一次调用的时,也就是代码 NotifyThread.Start(),这个时刻NotificationEvent还没有被初始化,而在WaitThread中就直接调用了这个变量!而Visual Studio的强大之处在于,在Debug模式下,CLR可能优先执行了主线程,也就是这个变量随即被初始化了,导致这个问题无法被发觉。而在Release模式下,无论怎么写Catch,这个异常都会直接终止程序。
修正这个Bug非常简单:
// Create a notification event, for when the sound stops playing
NotificationEvent = new AutoResetEvent(false);
NotifyThread.Start();
DirectX SDK的版本: August 2007.
代码(位于文件MainForm.cs):
void InitNotifications()
{
//-----------------------------------------------------------------------------
// Name: InitNotifications()
// Desc: Inits the notifications on the capture buffer which are handled
// in the notify thread.
//-----------------------------------------------------------------------------
if (null == applicationBuffer)
throw new NullReferenceException();
// Create a thread to monitor the notify events
if (null == NotifyThread)
{
NotifyThread = new Thread(new ThreadStart(WaitThread));
Capturing = true;
NotifyThread.Start();
// Create a notification event, for when the sound stops playing
NotificationEvent = new AutoResetEvent(false);
}
// Setup the notification positions
for (int i = 0; i < NumberRecordNotifications; i++)
{
PositionNotify[i].Offset = (NotifySize * i) + NotifySize - 1;
PositionNotify[i].EventNotifyHandle = NotificationEvent.Handle;
}
applicationNotify = new Notify(applicationBuffer);
// Tell DirectSound when to notify the app. The notification will come in the from
// of signaled events that are handled in the notify thread.
applicationNotify.SetNotificationPositions(PositionNotify, NumberRecordNotifications);
}
private void WaitThread()
{
while(Capturing)
{
//Sit here and wait for a message to arrive
NotificationEvent.WaitOne(Timeout.Infinite, true);
RecordCapturedData();
}
}
注意,在WaitThread被第一次调用的时,也就是代码 NotifyThread.Start(),这个时刻NotificationEvent还没有被初始化,而在WaitThread中就直接调用了这个变量!而Visual Studio的强大之处在于,在Debug模式下,CLR可能优先执行了主线程,也就是这个变量随即被初始化了,导致这个问题无法被发觉。而在Release模式下,无论怎么写Catch,这个异常都会直接终止程序。
修正这个Bug非常简单:
// Create a notification event, for when the sound stops playing
NotificationEvent = new AutoResetEvent(false);
NotifyThread.Start();
相关文章推荐
- Erlang 程序引发共享内存 bug 的一个例子
- Erlang 程序引发共享内存 bug 的一个例子
- 一个移位操作引发的程序Bug
- 一个LoadLibrary导致程序死机的Bug的诊断
- [数据库测试]强烈推荐一个python ODBC数据源插件,可支持Oracle,Db2,Mysql,Sql-server以及各种数据库版本,附例子和测试程序
- 一个简单的GTK的例子程序
- VS2008编译DirectX SDK 2004 summer版的Sound例子程序出现的libcp.lib找不到的问题的解决办法
- Linux下C程序调用.so(动态链)的一个例子
- 【原创】关于程序卸载的一个Bug
- SuspendThread 造成程序死锁的一个例子
- 微信小程序例子——使用画布组件绘制一个长200px,宽100px的长方形
- ABAP--一个极好的调用外部java程序的Search Help Exit的实例(RFC好例子)
- J2ME程序开发环境配置的一个简单例子
- 发现一个规律:如果google上搜不到程序报错原因,意味着隐藏着一个非常2B的bug
- asp.net 的一个新bug. 当你的程序出问题时, 不仅仅要检查自己的代码和逻辑, 还要检查微软的代码, 累吗?
- 一个C++程序重构的例子——糟糕的代码
- ABAP--一个极好的调用外部java程序的Search Help Exit的实例(RFC好例子)
- VC程序的安装与部署安装项目版-一个简单例子
- 使用rpcgen构建分布式程序的一个简单例子
- [数据库测试]强烈推荐一个python ODBC数据源插件,可支持Oracle,Db2,Mysql,Sql-server以及各种数据库版本,附例子和测试程序