{"id":1773,"date":"2017-07-11T17:08:57","date_gmt":"2017-07-11T15:08:57","guid":{"rendered":"https:\/\/alax.info\/blog\/?p=1773"},"modified":"2017-07-11T17:34:57","modified_gmt":"2017-07-11T15:34:57","slug":"imfattributescopyallitems-freeze-on-copying-to-self","status":"publish","type":"post","link":"https:\/\/alax.info\/blog\/1773","title":{"rendered":"IMFAttributes::CopyAllItems freeze on copying to self"},"content":{"rendered":"<p>An attempt to copy Media Foundation attribute collection to itself results in a deadlock. Well, it&#8217;s not a good idea and a practical one to do a nonsense like this, but the implementation should be resistant to such use either, esp. avoiding the unexpected deadlock.<\/p>\n<pre><code>#include \"stdafx.h\"\r\n#include &lt;mfapi.h&gt;\r\n\r\n#pragma comment(lib, \"mfplat.lib\")\r\n#pragma comment(lib, \"mfuuid.lib\")\r\n\r\nint main()\r\n{\r\n    _ATLTRY\r\n    {\r\n        ATLENSURE_SUCCEEDED(MFStartup(MF_VERSION));\r\n        CComPtr&lt;IMFAttributes&gt; pAttributes;\r\n        ATLENSURE_SUCCEEDED(MFCreateAttributes(&amp;pAttributes, 1));\r\n        ATLENSURE_SUCCEEDED(pAttributes-&gt;SetGUID(MF_MT_SUBTYPE, GUID_NULL));\r\n        ATLENSURE_SUCCEEDED(pAttributes-&gt;CopyAllItems(pAttributes)); \/\/ &lt;&lt;--- Freeze\r\n    }\r\n    _ATLCATCHALL()\r\n    {\r\n    }\r\n    return 0;\r\n}\r\n<\/code><\/pre>\n<p>The freeze takes place around SRW locks, that is presumably the implementation attempts to lock the data for reading and then once again for writing immediately afterwards.<\/p>\n<pre><code>    ntdll.dll!_NtWaitForAlertByThreadId@8\u001e()    Unknown\r\n    ntdll.dll!RtlAcquireSRWLockExclusive()  Unknown\r\n    mfplat.dll!CMFAttributesImpl&lt;struct IMFAttributes,class CMFSRWLock&gt;::DeleteAllItems(void)   Unknown\r\n    mfplat.dll!CMFAttributesImpl&lt;struct IMFAttributes,class CMFSRWLock&gt;::_CloneAllAttributes(struct IMFAttributes *)    Unknown\r\n    mfplat.dll!CMFAttributesImpl&lt;struct IMFAttributes,class CMFSRWLock&gt;::CopyAllItems(struct IMFAttributes *)   Unknown\r\n&gt;   MfSample01.exe!main() Line 15   C++\r\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>An attempt to copy Media Foundation attribute collection to itself results in a deadlock. Well, it&#8217;s not a good idea and a practical one to do a nonsense like this, but the implementation should be resistant to such use either, esp. avoiding the unexpected deadlock. #include &#8220;stdafx.h&#8221; #include &lt;mfapi.h&gt; #pragma comment(lib, &#8220;mfplat.lib&#8221;) #pragma comment(lib, &#8220;mfuuid.lib&#8221;)&hellip; <\/p>\n<p><a class=\"moretag\" href=\"https:\/\/alax.info\/blog\/1773\">Read the full article<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[63,424,70],"class_list":["post-1773","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-bug","tag-media-foundation","tag-winapi"],"_links":{"self":[{"href":"https:\/\/alax.info\/blog\/wp-json\/wp\/v2\/posts\/1773","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/alax.info\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/alax.info\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/alax.info\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/alax.info\/blog\/wp-json\/wp\/v2\/comments?post=1773"}],"version-history":[{"count":0,"href":"https:\/\/alax.info\/blog\/wp-json\/wp\/v2\/posts\/1773\/revisions"}],"wp:attachment":[{"href":"https:\/\/alax.info\/blog\/wp-json\/wp\/v2\/media?parent=1773"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/alax.info\/blog\/wp-json\/wp\/v2\/categories?post=1773"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/alax.info\/blog\/wp-json\/wp\/v2\/tags?post=1773"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}