{"id":2070,"date":"2020-06-17T16:01:57","date_gmt":"2020-06-17T14:01:57","guid":{"rendered":"https:\/\/alax.info\/blog\/?p=2070"},"modified":"2020-06-17T16:02:49","modified_gmt":"2020-06-17T14:02:49","slug":"something-got-broken-around-version-16-6-1-of-visual-c-compiler","status":"publish","type":"post","link":"https:\/\/alax.info\/blog\/2070","title":{"rendered":"Something got broken around version 16.6.1 of Visual C++ compiler"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Ancient piece of code started giving troubles:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>template &lt;typename T>\nclass ATL_NO_VTABLE CMediaControlT :\n    ...\n{\n    ...\n    STDMETHOD(Run)()\n    {\n        ...\n        T* MSVC_FIX_VOLATILE pT = static_cast&lt;T*>(this); \/\/ &lt;&lt;--------------------------------\n        CRoCriticalSectionLock GraphLock(pT->m_GraphCriticalSection);\n        pT->FilterGraphNeeded();\n        __D(pT->GetMediaControl(), E_NOINTERFACE);\n        pT->PrepareCue();\n        pT->DoRun();\n        __if_exists(T::Fire_Running)\n        {\n            pT->Fire_Running();\n        }\n    ...\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">When <code>MSVC_FIX_VOLATILE<\/code> is nothing, it appears that optimizing compiler forgets <code>pT<\/code> and uses just some variation of adjusted <code>this<\/code>, which makes sense overall because static cast between the two can be resolved at compile time.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">However, the problem is that the value of <code>this<\/code> is wrong and there is undefined behavior scenario.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If I make <code>MSVC_FIX_VOLATILE<\/code> to be <code>volatile<\/code> and have the variable <code>pT<\/code> somewhat &#8220;heavier&#8221;, optimizing compiler would forget <code>this<\/code> and uses <code>pT<\/code> directly with everything working as expected.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The problem still exists in current 16.6.2.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Ancient piece of code started giving troubles: When MSVC_FIX_VOLATILE is nothing, it appears that optimizing compiler forgets pT and uses just some variation of adjusted this, which makes sense overall because static cast between the two can be resolved at compile time. However, the problem is that the value of this is wrong and there&hellip; <\/p>\n<p><a class=\"moretag\" href=\"https:\/\/alax.info\/blog\/2070\">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":[38,58],"class_list":["post-2070","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-c","tag-visual-studio"],"_links":{"self":[{"href":"https:\/\/alax.info\/blog\/wp-json\/wp\/v2\/posts\/2070","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=2070"}],"version-history":[{"count":0,"href":"https:\/\/alax.info\/blog\/wp-json\/wp\/v2\/posts\/2070\/revisions"}],"wp:attachment":[{"href":"https:\/\/alax.info\/blog\/wp-json\/wp\/v2\/media?parent=2070"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/alax.info\/blog\/wp-json\/wp\/v2\/categories?post=2070"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/alax.info\/blog\/wp-json\/wp\/v2\/tags?post=2070"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}