<?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>Wait, you never said that I had to initialize the object before I used it!</title><link>http://blogs.msdn.com/b/oldnewthing/archive/2012/08/23/10342533.aspx</link><description>Some things are so obvious they go without saying.</description><dc:language>en-US</dc:language><generator>Telligent Evolution Platform Developer Build (Build: 5.6.50428.7875)</generator><item><title>re: Wait, you never said that I had to initialize the object before I used it!</title><link>http://blogs.msdn.com/b/oldnewthing/archive/2012/08/23/10342533.aspx#10343828</link><pubDate>Mon, 27 Aug 2012 14:18:50 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10343828</guid><dc:creator>JamesJohnston</dc:creator><description>&lt;p&gt;Why did they make every object in .NET / Java an object, anyway? &amp;nbsp;It seems like that needlessly complicates the runtime, when a class in the base library called &amp;quot;Monitor&amp;quot; could have done the job just as well. &amp;nbsp;As an added bonus, it forces the user to specify that functionality in a scope. &amp;nbsp;As it is, who knows what objects are being used as locks and what are not?&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10343828" width="1" height="1"&gt;</description></item><item><title>re: Wait, you never said that I had to initialize the object before I used it!</title><link>http://blogs.msdn.com/b/oldnewthing/archive/2012/08/23/10342533.aspx#10343673</link><pubDate>Sun, 26 Aug 2012 21:33:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10343673</guid><dc:creator>Joshua</dc:creator><description>&lt;p&gt;@Carl D. Last I checked, the slab locks that I prefer to use require only zero initialization and no cleanup, and when not being waited on way two pointers in size (actually an atomic_t followed by a pointer but padding will push it to two pointers in the rare architecture where that&amp;#39;s different).&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10343673" width="1" height="1"&gt;</description></item><item><title>re: Wait, you never said that I had to initialize the object before I used it!</title><link>http://blogs.msdn.com/b/oldnewthing/archive/2012/08/23/10342533.aspx#10343672</link><pubDate>Sun, 26 Aug 2012 21:24:47 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10343672</guid><dc:creator>arm ram</dc:creator><description>&lt;p&gt;@alegr1: Good luck cooding on WindowsRT.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10343672" width="1" height="1"&gt;</description></item><item><title>re: Wait, you never said that I had to initialize the object before I used it!</title><link>http://blogs.msdn.com/b/oldnewthing/archive/2012/08/23/10342533.aspx#10343326</link><pubDate>Fri, 24 Aug 2012 16:22:45 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10343326</guid><dc:creator>John Doe</dc:creator><description>&lt;p&gt;«MSDN doesn&amp;#39;t say &amp;quot;You must initialize an SRW lock before using it&amp;quot; because the statement was believed to be so obvious that it never occurred to anybody that somebody would think the opposite was true. I mean, what&amp;#39;s the point of having an Initialize­SRW­Lock function if initialization is not required? Think of it as one of the ground rules for programming: If an object has an initialization method, you must initialize the object before using it.»&lt;/p&gt;
&lt;p&gt;@Raymond, not everyone knows the Windows API as you do. I dare to say most people don&amp;#39;t, actually. It may seem very logic to you that the mere presence of an Init* function in the reference is enough to imply that it must be used. I could say the same about the API&amp;#39;s I or my colleagues develop to people outside the department or even the company.&lt;/p&gt;
&lt;p&gt;I say it should be explicitly there. What&amp;#39;s not there is not to take for granted. Of course, there are limits to this, such as passing strings that are not zero-terminated to C API&amp;#39;s that deal with char * (and the likes of it). But imagine that there was no string convention for C; then at least the latter would have to be specified somewhere, either in each function reference or in an introductory chapter stating that &amp;quot;every char * parameter and return value is a zero terminated string&amp;quot;.&lt;/p&gt;
&lt;p&gt;I regard the MSDN library as a very good quality documentation library, actually one of the best you can read and navigate online. It often states how to initialize many structures, mainly the first field of structures that are expected to evolve between versions of Windows. These don&amp;#39;t have an initialization function, so they kind of have to say how to initialize. Others don&amp;#39;t need initialization, such as an array of structs that is filled by some API function. Yet others have initialization functions, such as VariantInit:&lt;/p&gt;
&lt;p&gt;«Before use, all VARIANTARGs must be initialized by VariantInit.»&lt;/p&gt;
&lt;p&gt;Why should the synchronization section be any different from others in this regard? Why not say how and when to initialize the objects it handles? The user doesn&amp;#39;t know what&amp;#39;s required unless it&amp;#39;s stated. Try to put yourself on the other side.&lt;/p&gt;
&lt;p&gt;Disclaimer: I belong to that group of people that doesn&amp;#39;t know the Windows API that intimately, so there may be other sections that also lack documentation about required initializations. I myself may have taken some initializations for granted just because I&amp;#39;ve seen an Init* function in the reference section.&lt;/p&gt;
&lt;p&gt;Disclaimer: My intention is not to point a finger to the MSDN library or the synchronization section, I&amp;#39;m just keeping the subject of conversation.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10343326" width="1" height="1"&gt;</description></item><item><title>re: Wait, you never said that I had to initialize the object before I used it!</title><link>http://blogs.msdn.com/b/oldnewthing/archive/2012/08/23/10342533.aspx#10343247</link><pubDate>Fri, 24 Aug 2012 11:55:20 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10343247</guid><dc:creator>jas88</dc:creator><description>&lt;p&gt;&amp;quot;If newed variables didn&amp;#39;t need to be initialized, and local variables didn&amp;#39;t need to be initialized, then what&amp;#39;s the point of InitializeSRWLock? In the author&amp;#39;s mental model, you would never need to call it!&amp;quot;&lt;/p&gt;
&lt;p&gt;If they&amp;#39;re using a higher level wrapper, it seems reasonable to assume the wrapper calls InitializeSRWLock for you - after all, you aren&amp;#39;t explicitly calling any memory management functions either, but they are still documented and involved behind the scenes.&lt;/p&gt;
&lt;p&gt;In this particular case, the &amp;#39;memory leak&amp;#39; could pass for an optimization if it had a good PR team: it gets disposed of implicitly when the process exists anyway, and that automatic disposal could well be more efficient. Not an issue here, but I&amp;#39;ve seen a few programs spent long enough to be quite irritating cleaning up all their internal state before exiting. (In particular, swapping pages back in just to dispose of their contents, when the OS could just have marked those pages free on exit without the excess I/O.)&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10343247" width="1" height="1"&gt;</description></item><item><title>re: Wait, you never said that I had to initialize the object before I used it!</title><link>http://blogs.msdn.com/b/oldnewthing/archive/2012/08/23/10342533.aspx#10343189</link><pubDate>Fri, 24 Aug 2012 08:59:30 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10343189</guid><dc:creator>Matt</dc:creator><description>&lt;p&gt;@Eugene When in doubt, Microsoft uses the WINAPI calling convention. There are some exceptions (like printf) but by-and-large, expect all Microsoft functions to be WINAPI.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10343189" width="1" height="1"&gt;</description></item><item><title>re: Wait, you never said that I had to initialize the object before I used it!</title><link>http://blogs.msdn.com/b/oldnewthing/archive/2012/08/23/10342533.aspx#10343185</link><pubDate>Fri, 24 Aug 2012 08:51:31 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10343185</guid><dc:creator>Matt</dc:creator><description>&lt;p&gt;On the subject of Pinvoking, the default calling convention is WINAPI (which is __stdcall on x86), and it transparently handles the W/A case (under the hood it tries GetProcAddress(module, name) - if that fails, then it tries GetProcAddress(module, name+W) and sets the conversion of strings to unicode, and if that fails it tries GetProcAddress(module, name+A) and sets the conversion mode to ascii).&lt;/p&gt;
&lt;p&gt;BONUS: This logic is the same as the logic used by rundll32.exe :)&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10343185" width="1" height="1"&gt;</description></item><item><title>re: Wait, you never said that I had to initialize the object before I used it!</title><link>http://blogs.msdn.com/b/oldnewthing/archive/2012/08/23/10342533.aspx#10343113</link><pubDate>Fri, 24 Aug 2012 02:29:18 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10343113</guid><dc:creator>Crescens2k</dc:creator><description>&lt;p&gt;@Eugene&lt;/p&gt;
&lt;p&gt;Even still, it is well known that the MSDN is for documenting, and if you have found some functions that do it then it is best not to assume that everyone will put in the calling convention. Oh, and it isn&amp;#39;t related to the shell team either, look at the WDK documentation, none of them include the calling convention either. So even though it is their policy, not all of the teams stick to it.&lt;/p&gt;
&lt;p&gt;I agree with Raymond, if you want to re-prototype a function, get the prototype from the header, that is the only way you are guaranteed accuracy. This is how I normally do it when I need to access a small amount of backwards incompatable functions. If I need to use a larger amount, I tend to write DLLs that wrap the behaviour I want and dynamically load it after a version check.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10343113" width="1" height="1"&gt;</description></item><item><title>re: Wait, you never said that I had to initialize the object before I used it!</title><link>http://blogs.msdn.com/b/oldnewthing/archive/2012/08/23/10342533.aspx#10343065</link><pubDate>Thu, 23 Aug 2012 22:50:25 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10343065</guid><dc:creator>Carl D</dc:creator><description>&lt;p&gt;@Gabe&lt;/p&gt;
&lt;p&gt;While the CLR and JVM logically have a lock per object, neither of them is actually implemented that way. &amp;nbsp;Both use a pool of locks that are associated with objects on an as-needed basis. &amp;nbsp;Since the vast majority of objects are never waited on, this saves resources on an enormous scale.&lt;/p&gt;
&lt;p&gt;A quality design for an in-memory database would likely involve a similar level of indirection and not actually allocate a lock per row.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10343065" width="1" height="1"&gt;</description></item><item><title>re: Wait, you never said that I had to initialize the object before I used it!</title><link>http://blogs.msdn.com/b/oldnewthing/archive/2012/08/23/10342533.aspx#10343056</link><pubDate>Thu, 23 Aug 2012 22:38:33 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10343056</guid><dc:creator>Eugene</dc:creator><description>&lt;p&gt;@Raymond&lt;/p&gt;
&lt;p&gt;I need my application to work on WinXP, so I cannot link directly to Win7-only functions. I have to GetProcAddress it, so prototype doesn&amp;#39;t help much.&lt;/p&gt;
&lt;p&gt;And MSDN used to include calling convention in prototypes. Take any old function: LoadLibrary, CreateFile, EnterCriticalSection etc. Or SRW functions mentioned in this post -- all have WINAPI, which is a calling convention macro. In fact, the only functions without calling convention that I can find in a couple of minutes are from Shell32.dll. So this looks more like Shell Team policy rather than general MSDN policy.&lt;/p&gt;
&lt;div class="post"&gt;[&lt;i&gt;if you&amp;#39;re going to re-prototype a function, get it from the header file. That&amp;#39;s where the compiler gets it. (Or use &lt;code&gt;decltype&lt;/code&gt;.) The MSDN documentation is presented for exposition, not for handing directly to a compiler. (SAL annotations are often stripped down or removed entirely, and separate prototypes are not given for A/W variants.) -Raymond&lt;/i&gt;]&lt;/div&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10343056" width="1" height="1"&gt;</description></item></channel></rss>