DirectShow code crashes after exit (PushSourceDesktop sample)

不打扰是莪最后的温柔 提交于 2019-11-28 14:28:10

The big problem is that you have to call CoUninitialize only after all COM pointers are released. Now that you are using raw pointers instead of CComPtr-like smart templates, your code both is poorly readable, and it is so easy to make a mistake and forget to release one of the pointers. CoUninitialize cleans things up and then later on it appears that some COM object is still alive and it quickly gets into trouble and crashes your app.

Additionally to this, I don't see a reason for you to use COINIT_MULTITHREADED apartment. To stay away of trouble, you should rather do all top-level management on filter graphs from STA thread. Streaming and worker threads will be MTA, and it is fine.

ATL offers CComPtr templates well described on MSDN. DirectShow BaseClasses offer you a lightweight analog QzCComPtr which I suggest you start using for your own convenience.

Your code will look like this:

CoInitialize(...);
{
  CComPtr<IFooA> pFooA;
  CComPtr<IFooB> pFooB;
  // ...
}
CoUninitialize();

The idea is that all ~CComPtr are done before code reaches CoUninitialize.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!