DirectShow code crashes after exit (PushSourceDesk

2020-02-07 06:57发布

问题:

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 and saves it to a file. But the application crashes when its done/exits.

When I break on the error it only says no source available and the call stack location is KernelBase.dll!7560280C().

I thought I give it a shot here to see if anyone can recognize some issue or if I am doing something blatantly wrong that might cause the crash. Thanks in advance!

EDIT: working code

int main()
{

HRESULT hr;

hr = CoInitialize(NULL);
{
CComPtr<IBaseFilter> pMux;
CComPtr<IMediaControl> pMC;
CComPtr<IBaseFilter> pFilterr;
CComPtr<IGraphBuilder> pGraph;
CComPtr<ICaptureGraphBuilder2> pBuild;
CComPtr<IUnknown> pUnk;

hr = CoCreateInstance(CLSID_CaptureGraphBuilder2, NULL, CLSCTX_INPROC_SERVER,
    IID_ICaptureGraphBuilder2, (void**)&pBuild);


hr = CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, IID_IGraphBuilder, (void**)(&pGraph));

hr = pBuild->SetFiltergraph(pGraph);


static CLSID const clsid = {0x4ea6930a, 0x2c8a, 0x4ae6, {0xa5, 0x61, 0x56, 0xe4, 0xb5, 0x4, 0x44, 0x37}}; //Pushsourcedesktop

 hr = CreateObjectFromPath(TEXT("c:\\filters\\PushSource.dll"), clsid, &pUnk);

 pFilterr = pUnk;

if (SUCCEEDED(hr))
{
    HRESULT hr = pGraph->AddFilter(pFilterr, L"Private Filter");
}

hr = pBuild->SetOutputFileName(
    &MEDIASUBTYPE_Avi,  // Specifies AVI for the target file.
    L"C:\\wav\\Example2.avi", // File name.
    (IBaseFilter**)&pMux,              // Receives a pointer to the mux.
    NULL); 


hr = pBuild->RenderStream(
    NULL,//PIN_CATEGORY_CAPTURE,//NULL,//&PIN_CATEGORY_CAPTURE, // Pin category.
    NULL,//&MEDIATYPE_Video,//&MEDIATYPE_Interleaved,//NULL,//&MEDIATYPE_Audio,      // Media type.
    pFilterr,//pSrc,           // Capture filter.
   NULL,//pCompression2, //pCompression,                  // Intermediate filter (optional).
    (IBaseFilter*)pMux);                 // Mux or file sink filter.


    hr = pGraph->QueryInterface(IID_IMediaControl, (void **)&pMC);
    printf("START");  


    pMC->Pause();
    hr = pMC->Run();
    Sleep(4000);
      hr = pMC->Stop();
    printf("END");  

}
CoUninitialize();
return 0;
}

回答1:

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.