From: Derek Lesho Subject: Re: [PATCH 5/6] mfmediaengine: Close the media session on shutdown. Message-Id: <4d83a806-1216-0dfc-bfb1-9e0d8f317d8d@codeweavers.com> Date: Wed, 5 Jan 2022 11:03:05 -0500 In-Reply-To: References: <20220104173527.77306-1-dlesho@codeweavers.com> <20220104173527.77306-5-dlesho@codeweavers.com> On 1/5/22 06:09, Nikolay Sivov wrote: > > > On 1/4/22 20:35, Derek Lesho wrote: >>     failed: >> @@ -1297,6 +1302,7 @@ static void free_media_engine(struct >> media_engine *engine) >>       } >>       SysFreeString(engine->current_source); >>       DeleteCriticalSection(&engine->cs); >> +    CloseHandle(engine->session_closed); >>       free(engine->video_frame.buffer); >>       free(engine); >>   } >> @@ -1956,6 +1962,8 @@ static HRESULT WINAPI >> media_engine_Shutdown(IMFMediaEngine *iface) >>       else >>       { >>           media_engine_set_flag(engine, FLAGS_ENGINE_SHUT_DOWN, TRUE); >> +        IMFMediaSession_Close(engine->session); >> +        WaitForSingleObject(engine->session_closed, INFINITE); >>           IMFMediaSession_Shutdown(engine->session); >>       } > > Could you explain what does this fix? ::Close releases the queued topology object, ::Shutdown doesn't. ::Release also closes the topology object, but there's a circular dependency here as the sample grabber sink node in the queued topology references the sample grabber media sink object, which references the sample grabber callback object, which, in the case of the media engine, contributes to the reference count of the media engine, and the media engine only releases the media session once it is fully released. Additionally, the documentation advises that you ::Close a session before shutting it down. https://docs.microsoft.com/en-us/windows/win32/medfound/step-7--shut-down-the-media-session >> LeaveCriticalSection(&engine->cs); >> @@ -2451,6 +2459,7 @@ static HRESULT init_media_engine(DWORD flags, >> IMFAttributes *attributes, struct >>       engine->duration = NAN; >>       engine->video_frame.pts = MINLONGLONG; >>       InitializeCriticalSection(&engine->cs); >> +    engine->session_closed = CreateEventW(NULL, TRUE, FALSE, NULL); >>         hr = IMFAttributes_GetUnknown(attributes, >> &MF_MEDIA_ENGINE_CALLBACK, &IID_IMFMediaEngineNotify, >>               (void **)&engine->callback); > >