DirectShow 代码在退出后崩溃(PushSourceDesktop 示例) [英] DirectShow code crashes after exit (PushSourceDesktop sample)

查看:17
本文介绍了DirectShow 代码在退出后崩溃(PushSourceDesktop 示例)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 SDK (PushSourceDesktop) 附带的桌面捕获过滤器.我编译它并且似乎成功使用它,因为它实际上捕获了我的桌面并将其保存到文件中.但是应用程序在完成/退出时崩溃.

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.

当我中断错误时,它只说没有可用的源,调用堆栈位置是 KernelBase.dll!7560280C().

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!

工作代码

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;
}

推荐答案

的问题是你必须在所有 COM 指针释放后才调用 CoUninitialize.现在您使用的是原始指针而不是类似 CComPtr 的智能模板,您的代码都可读性差,而且很容易出错而忘记释放其中一个指针.CoUninitialize 清理了一些东西,然后看起来一些 COM 对象仍然存在,它很快就会遇到问题并让你的应用程序崩溃.

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.

除此之外,我看不出你有理由使用 COINIT_MULTITHREADED 公寓.为了避免麻烦,您应该对来自 STA 线程的过滤器图进行所有顶级管理.流和工作线程将是 MTA,这很好.

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 提供 CComPtr 模板在 MSDN 上有很好的描述.DirectShow BaseClasses 为您提供了一个轻量级的模拟 QzCComPtr,我建议您开始使用它以方便您自己.

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.

您的代码将如下所示:

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

这个想法是所有 ~CComPtr 在代码到达 CoUninitialize 之前完成.

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

这篇关于DirectShow 代码在退出后崩溃(PushSourceDesktop 示例)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆