DirectShow code crashes after exit (PushSourceDesktop sample)

前端 未结 1 931
悲&欢浪女
悲&欢浪女 2020-12-12 03:05

I\'m trying to use the Desktop capture filter that comes with the SDK (PushSourceDesktop). I compiled it and seem to use it successfully as it actually captures my desktop a

相关标签:
1条回答
  • 2020-12-12 03:32

    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.

    0 讨论(0)
提交回复
热议问题