<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>What's wrong with this code, part 6 - the answers</title><link>http://blogs.msdn.com/larryosterman/archive/2004/09/09/227490.aspx</link><description>In yesterdays post I presented a trace log writer that had a subtle bug. As I mentioned, the problem had nothing to do with the code, but instead, the problem had to do with the directory in which the trace log file was written. My second hint to the</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>re: What's wrong with this code, part 6 - the answers</title><link>http://blogs.msdn.com/larryosterman/archive/2004/09/09/227490.aspx#227594</link><pubDate>Thu, 09 Sep 2004 21:26:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:227594</guid><dc:creator>Pavel Lebedinsky</dc:creator><description>&amp;gt; Consider a folder with an ACL that allows&lt;br&gt;&amp;gt; the user to create files, but not write&lt;br&gt;&amp;gt; to them.&lt;br&gt;&lt;br&gt;Note however that users can always change permissions on any files they created, regardless of what the DACL says.&lt;br&gt;&lt;br&gt;Your review folder example will work only if you apply additional restrictions at the share level, to prevent users from changing the DACL.&lt;br&gt;&lt;br&gt;&amp;gt; EnterCriticalSection should be protected&lt;br&gt;&amp;gt; with a try/catch since it can throw&lt;br&gt;&lt;br&gt;No it shouldn't. On XP and later, Enter/LeaveCS never raise exceptions. On Win2K and before, they can raise, but you can't recover from this, even if you use try/except (try/catch should only be used for C+ exceptions; in Whidbey catch(...) will not even catch SEH exceptions by default).&lt;br&gt;&lt;br&gt;The reason you can't recover is because on Win2K the critical section could be left in a corrupted state when Enter or LeaveCriticalSection throws.&lt;br&gt;&lt;br&gt;The bottom line is that you should never wrap Enter/LeaveCriticalSection in a try/except. On XP and later, it's not necessary, and on Win2K it doesn't really help anyway.&lt;br&gt;&lt;br&gt;If you need critical sections to work reliably in out-of-memory conditions on Win2K your only option is to preallocate the event using InitializeCriticalSectionAndSpinCount.&lt;br&gt;&lt;br&gt;</description></item><item><title>re: What's wrong with this code, part 6 - the answers</title><link>http://blogs.msdn.com/larryosterman/archive/2004/09/09/227490.aspx#227606</link><pubDate>Thu, 09 Sep 2004 21:41:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:227606</guid><dc:creator>Larry Osterman</dc:creator><description>A good point Pavel.&lt;br&gt;&lt;br&gt;And I meant try/except, not try/catch, I'll update the text.&lt;br&gt;&lt;br&gt;</description></item><item><title>re: What's wrong with this code, part 6 - the answers</title><link>http://blogs.msdn.com/larryosterman/archive/2004/09/09/227490.aspx#228077</link><pubDate>Fri, 10 Sep 2004 21:55:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:228077</guid><dc:creator>Skywing</dc:creator><description>I never said it raises on Windows XP or later.  In those cases it uses a system wide KeyedEvent (new kernel object) to avoid having to allocate memory for each critical section having it's own wait semaphore.&lt;br&gt;&lt;br&gt;Note that MSDN *RECOMMENDS* that you use SEH to catch EnterCriticalSection exceptions ( &lt;a target="_new" href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/entercriticalsection.asp"&gt;http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/entercriticalsection.asp&lt;/a&gt; ).&lt;br&gt;&lt;br&gt;How do you arrive at the conclusion that the critical section will be put into an invalid state if an exception is thrown?  RtlLeaveCriticalSection throws an exception if NtSetEvent fails, which can only happen if the structure itself is damaged or somebody messed with the event handle (which are obviously nonrecoverable, but unrelated to running out of memory).  I didn't write the critsec code, but I don't *think* the lockcount being incremented an extra time will cause any fatal problems -- perhaps a lock cmpxchg with 0 when it's not really necessary, but that oughtn't break anything.</description></item><item><title>re: What's wrong with this code, part 6 - the answers</title><link>http://blogs.msdn.com/larryosterman/archive/2004/09/09/227490.aspx#228089</link><pubDate>Fri, 10 Sep 2004 22:56:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:228089</guid><dc:creator>Pavel Lebedinsky</dc:creator><description>&amp;gt; How do you arrive at the conclusion that&lt;br&gt;&amp;gt; the critical section will be put into an&lt;br&gt;&amp;gt; invalid state if an exception is thrown?&lt;br&gt;&lt;br&gt;Here are some quotes from Neill Clift (he is the guy who fixed critical sections for XP):&lt;br&gt;&lt;br&gt;&amp;quot;It was really impossible to handle exceptions generated by EnterCriticalSection and LeaveCriticalSection because they left the CS in a state you couldn't recover from.&amp;quot;&lt;br&gt;&lt;br&gt;&lt;a target="_new" href="http://groups.google.com/groups?hl=en&amp;amp;lr=&amp;amp;ie=UTF-8&amp;amp;selm=utHsttTmBHA.2432%40tkmsftngp07"&gt;http://groups.google.com/groups?hl=en&amp;amp;lr=&amp;amp;ie=UTF-8&amp;amp;selm=utHsttTmBHA.2432%40tkmsftngp07&lt;/a&gt;&lt;br&gt;&lt;br&gt;&amp;quot;I considered it impossible to code to Enter/Leave raising. To make matters worse the internal structure of the lock was damaged by the raise such that it might be unusable after the event.&amp;quot;&lt;br&gt;&lt;br&gt;&lt;a target="_new" href="http://groups.google.com/groups?selm=3fe1d456%241%40news.microsoft.com&amp;amp;output=gplain"&gt;http://groups.google.com/groups?selm=3fe1d456%241%40news.microsoft.com&amp;amp;output=gplain&lt;/a&gt;&lt;br&gt;&lt;br&gt;The fact that LeaveCriticalSection can also raise in low memory situations (on Win2K) seems unintuitive at first but it's true - there's some obscure scenario where allocation of the event has to happen in LeaveCriticalSection instead of EnterCriticalSection.</description></item><item><title>re: What's wrong with this code, part 6 - the answers</title><link>http://blogs.msdn.com/larryosterman/archive/2004/09/09/227490.aspx#228098</link><pubDate>Fri, 10 Sep 2004 23:09:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:228098</guid><dc:creator>Skywing</dc:creator><description>Ah, okay.  In that case, somebody should update the MSDN documentation to not recommend something which doesn't work. :)</description></item><item><title>Updating Application Data</title><link>http://blogs.msdn.com/larryosterman/archive/2004/09/09/227490.aspx#234305</link><pubDate>Sun, 26 Sep 2004 03:24:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:234305</guid><dc:creator>Don Kiely's Technical Blatherings</dc:creator><description>Updating Application Data</description></item></channel></rss>