{"id":6783,"date":"2012-08-23T07:00:00","date_gmt":"2012-08-23T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2012\/08\/23\/wait-you-never-said-that-i-had-to-initialize-the-object-before-i-used-it\/"},"modified":"2012-08-23T07:00:00","modified_gmt":"2012-08-23T07:00:00","slug":"wait-you-never-said-that-i-had-to-initialize-the-object-before-i-used-it","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20120823-00\/?p=6783","title":{"rendered":"Wait, you never said that I had to initialize the object before I used it!"},"content":{"rendered":"<p>\nA customer reported that they were having trouble creating\n<a HREF=\"http:\/\/msdn.microsoft.com\/en-us\/library\/windows\/desktop\/aa904937(v=vs.85).aspx\">\nslim reader\/writer locks<\/a>\nat runtime.\nThey simplified the issue to a short program:\n<\/p>\n<pre>\n#include &lt;windows.h&gt;\n#include &lt;iostream&gt;\nusing namespace std; \/\/ this is just a quick test app\nint a = 10;\n\/\/ This version works\nint working_version()\n{\n SRWLOCK lock;\n AcquireSRWLockExclusive(&amp;lock);\n cout&lt;&lt;\"Acquired exclusively\"&lt;&lt;endl;\n a++;\n ReleaseSRWLockExclusive(&amp;lock);\n}\n\/\/ This one doesn't\nint broken_version_1()\n{\n SRWLOCK *lock = new SRWLOCK;\n AcquireSRWLockExclusive(lock);\n cout&lt;&lt;\"Acquired exclusively\"&lt;&lt;endl;\n a++;\n ReleaseSRWLockExclusive(lock);\n \/\/ ignore the memory leak - this is just a quick test app\n}\n\/\/ This one doesn't either\nint broken_version_2()\n{\n SRWLOCK *lock = new SRWLOCK[2];\n AcquireSRWLockExclusive(&amp;lock[0]);\n cout&lt;&lt;\"Acquired exclusively\"&lt;&lt;endl;\n a++;\n ReleaseSRWLockExclusive(&amp;lock[0]);\n \/\/ ignore the memory leak - this is just a quick test app\n}\nint main(int argc, char **argv)\n{\n switch (argv[1][0]) {\n case '0': working_version(); break;\n case '1': broken_version_1(); break;\n case '2': broken_version_2(); break;\n }\n cout&lt;&lt;\"a=\"&lt;&lt;a&lt;&lt;endl;\n return 0;\n}\n<\/pre>\n<p>\n&#8220;What is the correct way of creating an <code>SRWLOCK<\/code>\nvia the <code>new<\/code> operator?&#8221;\n<\/p>\n<p>\nIt wasn&#8217;t long before somebody noted that nowhere in the code\nis the function\n<code>Initialize&shy;SRW&shy;Lock<\/code> called.\n<\/p>\n<p>\n&#8220;Oh, yeah, thanks for catching that.\nIt looks like one needs to initialize SRW locks which are\ncreated via the <code>new<\/code> operator.\nOtherwise it&#8217;s not required.&#8221;\n<\/p>\n<p>\nNo, the function is <i>always<\/i> required.\nIt&#8217;s just that you got lucky in the local variable case\nand the initial stack garbage looks enough like an initialized SRW lock\nthat you don&#8217;t notice the problem.\n<\/p>\n<p>\nMSDN doesn&#8217;t say\n&#8220;You must initialize an SRW lock before using it&#8221;\nbecause the statement was believed to be so obvious\nthat it never occurred to anybody that somebody would\nthink the opposite was true.\nI mean, what&#8217;s the point of having an\n<code>Initialize&shy;SRW&shy;Lock<\/code> function if initialization\nis not required?\nThink of it as one of the\n<a HREF=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2006\/03\/20\/555511.aspx\">\nground rules for programming<\/a>:\nIf an object has an initialization method,\nyou must initialize the object before using it.\n<\/p>\n<p>\nBut just to be sure, I&#8217;ve submitted a documentation change request\nto add the requirement.\n<\/p>\n<p>\n<b>Bonus chatter<\/b>:\nA common coding pattern is to wrap the low-level C-style object\ninside a C++style RAII-style object.\n<\/p>\n<p>\n<b>Bonus chatter 2<\/b>:\nIf you&#8217;re creating a highly-concurrent system, then you should\nprobably put each lock on its own cache line.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A customer reported that they were having trouble creating slim reader\/writer locks at runtime. They simplified the issue to a short program: #include &lt;windows.h&gt; #include &lt;iostream&gt; using namespace std; \/\/ this is just a quick test app int a = 10; \/\/ This version works int working_version() { SRWLOCK lock; AcquireSRWLockExclusive(&amp;lock); cout&lt;&lt;&#8220;Acquired exclusively&#8221;&lt;&lt;endl; a++; ReleaseSRWLockExclusive(&amp;lock); [&hellip;]<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-6783","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>A customer reported that they were having trouble creating slim reader\/writer locks at runtime. They simplified the issue to a short program: #include &lt;windows.h&gt; #include &lt;iostream&gt; using namespace std; \/\/ this is just a quick test app int a = 10; \/\/ This version works int working_version() { SRWLOCK lock; AcquireSRWLockExclusive(&amp;lock); cout&lt;&lt;&#8220;Acquired exclusively&#8221;&lt;&lt;endl; a++; ReleaseSRWLockExclusive(&amp;lock); [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/6783","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=6783"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/6783\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=6783"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=6783"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=6783"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}