使用DispEventAdvise时应用退出触发异常求助
Application Crash on Exit with COM Event Sink in DirectShow Filter
I've set up the following test environment based on Visual Studio 2015 and Windows SDK 7.1, consisting of three components:
- A C# COM-enabled project
TestCs(exposes aRunmethod and fires aNotifyEventevent) - A C++ DirectShow filter
TestFilter(hostsTestCs, developed from the TransInPlace sample) - A minimal C++ console app
TestAppthat only loadsTestFilter
When running in Debug mode, all functionality works correctly—events are handled properly—but the application crashes on exit. The call stack provides no useful information. If I comment out the DispEventAdvise related code, the app exits normally.
Crash Details
The crash occurs when the main function exits, right after CoUninitialize().
Code Implementations
C++ Console App (TestApp)
#include <windows.h> #include <initguid.h> #include <atlbase.h> #include <streams.h> // {E705C8D3-EE60-4012-9E83-3CFE4A11D1B5} DEFINE_GUID(CLSID_TestFilter, 0xe705c8d3, 0xee60, 0x4012, 0x9e, 0x83, 0x3c, 0xfe, 0x4a, 0x11, 0xd1, 0xb5); void Test() { CComPtr<IBaseFilter> pFilter; HRESULT hr = CoCreateInstance(CLSID_TestFilter, NULL, CLSCTX_INPROC_SERVER, IID_IBaseFilter, (void **)&pFilter); if (FAILED(hr)) return; } void main() { CoInitialize(NULL); Test(); CoUninitialize(); }
C# Project (TestCs) - Interface Definitions
using System; using System.Runtime.InteropServices; namespace TestCs { [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)] public interface IFireTestCsEvents { [DispId(1)] void NotifyEvent(string message); } public delegate void NotifyEventEventHandler(string message); public interface ITestCs : IDisposable { event NotifyEventEventHandler NotifyEvent; bool Run(string fileName); void Close(); } }
C# Project (TestCs) - Implementation
using System.Runtime.InteropServices; namespace TestCs { [ClassInterface(ClassInterfaceType.None)] [ComSourceInterfaces(typeof(IFireTestCsEvents))] public class TestCsImp : ITestCs { public event NotifyEventEventHandler NotifyEvent; public TestCsImp() { } public bool Run(string fileName) { NotifyEvent?.Invoke(fileName); return true; } public void Close() { } public void Dispose() { Close(); } } }
C++ Filter (TestFilter) - TestCsWrapper.h
#pragma once #import "TestCs.tlb" using namespace TestCs; class TestCsWrapper : public IDispEventSimpleImpl<1, TestCsWrapper, &__uuidof(IFireTestCsEvents)> { public: BEGIN_SINK_MAP(TestCsWrapper) SINK_ENTRY_INFO(1, __uuidof(IFireTestCsEvents), 0x1, HandleEvent, &NotifyEvent) END_SINK_MAP() void __stdcall HandleEvent(BSTR message); public: TestCsWrapper(); virtual ~TestCsWrapper(); bool Open(); void Close(); bool IsValid(); bool Load(const wchar_t *fileName); private: ITestCsPtr m_pTestCs; static _ATL_FUNC_INFO NotifyEvent; };
C++ Filter (TestFilter) - TestCsWrapper.cpp
#include "Pch.h" #include "TestCsWrapper.h" _ATL_FUNC_INFO TestCsWrapper::NotifyEvent = { CC_STDCALL, VT_EMPTY, 1, { VT_BSTR } }; #define CREATE_CRASH void __stdcall TestCsWrapper::HandleEvent(BSTR message) { ::SysFreeString(message); } TestCsWrapper::TestCsWrapper() { } TestCsWrapper::~TestCsWrapper() { Close(); } bool TestCsWrapper::Open() { m_pTestCs = ITestCsPtr(__uuidof(TestCsImp)); if(!IsValid()) return false; #ifdef CREATE_CRASH DispEventAdvise(m_pTestCs); #endif return true; } void TestCsWrapper::Close() { if(IsValid()) { #ifdef CREATE_CRASH DispEventUnadvise(m_pTestCs); #endif m_pTestCs->Close(); m_pTestCs->Release(); m_pTestCs.Detach(); } } bool TestCsWrapper::IsValid() { return (m_pTestCs != NULL); } bool TestCsWrapper::Load(const wchar_t *fileName) { if(!IsValid()) return false; BSTR bstr = ::SysAllocString(fileName); VARIANT_BOOL success = m_pTestCs->Run(bstr); ::SysFreeString(bstr); return success == VARIANT_TRUE; }
Filter Initialization
In the filter's CreateInstance method, I added:
bool TestFilter::Open() { if (!m_testCs.Open()) return false; m_testCs.Load(L"some messsage"); return true; }
I can provide the complete Visual Studio solution code if needed. I'm looking for help analyzing the root cause of this crash and finding a fix.
内容的提问来源于stack exchange,提问作者EliD
相关产品推荐
相关产品推荐

