<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Code for Concinnity &#187; Useful tips</title>
	<atom:link href="http://cfc.kizzx2.com/index.php/category/useful-tips/feed/" rel="self" type="application/rss+xml" />
	<link>http://cfc.kizzx2.com</link>
	<description>beautiful and elegant solutions</description>
	<lastBuildDate>Fri, 10 Feb 2012 13:24:20 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>&#8220;Pretty-printing&#8221; STL vector in Eclipse CDT</title>
		<link>http://cfc.kizzx2.com/index.php/pretty-printing-stl-vector-in-eclipse-cdt/</link>
		<comments>http://cfc.kizzx2.com/index.php/pretty-printing-stl-vector-in-eclipse-cdt/#comments</comments>
		<pubDate>Sun, 03 Jul 2011 04:37:40 +0000</pubDate>
		<dc:creator>kizzx2</dc:creator>
				<category><![CDATA[Useful tips]]></category>
		<category><![CDATA[eclipse-cdt c++ ide gdb debug]]></category>

		<guid isPermaLink="false">http://cfc.kizzx2.com/?p=287</guid>
		<description><![CDATA[It&#8217;s surprising to see why the C++ debugging scene in Mac/Linux is so behind Visual Studio. One definite strengths of MSVS is the ability to display STL vectors as arrays in the debug view. If you&#8217;re using Eclipse CDT on Mac, you&#8217;re probably stuck with GDB 6.8 which doesn&#8217;t come with pretty printing. I tried [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="margin-left: 10px; float: right;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fcfc.kizzx2.com%2Findex.php%2Fpretty-printing-stl-vector-in-eclipse-cdt%2F">
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fcfc.kizzx2.com%2Findex.php%2Fpretty-printing-stl-vector-in-eclipse-cdt%2F&amp;source=kizzx2&amp;style=normal&amp;b=2" height="61" width="50" />
			</a>
		</div><p>It&#8217;s surprising to see why the C++ debugging scene in Mac/Linux is so behind Visual Studio. One definite strengths of MSVS is the ability to display STL vectors as arrays in the debug view.</p>

<p>If you&#8217;re using Eclipse CDT on Mac, you&#8217;re probably stuck with GDB 6.8 which doesn&#8217;t come with pretty printing. I tried self building GDB 7.2 but Eclipse CDT 8.0 doesn&#8217;t seem to be able to interact well with it (please someone come and correct me if I&#8217;m wrong).</p>

<p>So here&#8217;s how to display <code>std::vector</code> nicely even if you&#8217;re using GDB 6.8:</p>

<p><a href="http://cfc.kizzx2.com/wp-content/uploads/2011/07/eclipse-cdt-debug-vector.png"><img src="http://cfc.kizzx2.com/wp-content/uploads/2011/07/eclipse-cdt-debug-vector-300x181.png" alt="" title="Pretty-printing std::vector in Eclipse CDT" width="300" height="181" class="size-medium wp-image-288" /></a></p>

<p>As you can see, the trick is to convert it to a pointer and to use the array syntax <code>*((int*)nums) @ 3</code></p>

<p>Enjoy <img src='http://cfc.kizzx2.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://cfc.kizzx2.com/index.php/pretty-printing-stl-vector-in-eclipse-cdt/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Tutorial: Using WinDBG to call arbitrary functions &#8212; WinDBG kung-fu series</title>
		<link>http://cfc.kizzx2.com/index.php/tutorial-using-windbg-to-call-arbitrary-functions-windbg-kung-fu-series/</link>
		<comments>http://cfc.kizzx2.com/index.php/tutorial-using-windbg-to-call-arbitrary-functions-windbg-kung-fu-series/#comments</comments>
		<pubDate>Tue, 26 Oct 2010 15:29:00 +0000</pubDate>
		<dc:creator>kizzx2</dc:creator>
				<category><![CDATA[Useful tips]]></category>
		<category><![CDATA[windbg debug windows advanced]]></category>

		<guid isPermaLink="false">http://cfc.kizzx2.com/?p=228</guid>
		<description><![CDATA[Most people developing for MS would praise how MSVS debugger is the best debugger out there (I doubt if many of them have actually used any other debuggers when they say that). That may be true for managed code debugging (.NET), but I just find that MSVS feels severely crippled when it comes to hard-core [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="margin-left: 10px; float: right;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fcfc.kizzx2.com%2Findex.php%2Ftutorial-using-windbg-to-call-arbitrary-functions-windbg-kung-fu-series%2F">
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fcfc.kizzx2.com%2Findex.php%2Ftutorial-using-windbg-to-call-arbitrary-functions-windbg-kung-fu-series%2F&amp;source=kizzx2&amp;style=normal&amp;b=2" height="61" width="50" />
			</a>
		</div><p>Most people developing for MS would praise how MSVS debugger is the best debugger out there (I doubt if many of them have actually used any other debuggers when they say that). That may be true for managed code debugging (.NET), but I just find that MSVS feels severely crippled when it comes to hard-core low level stuffs in C/C++.</p>

<p>This example shows you how to use the better tool for the job &#8212; WinDBG. While still severely crippled compared to GDB+Python scripting. Anyway&#8230;</p>

<h2>The example program</h2>

<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br /></div></td><td><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339900;">#include &lt;iostream&gt;</span><br />
<span style="color: #339900;">#include &lt;string&gt;</span><br />
<br />
<span style="color: #0000ff;">class</span> foo<br />
<span style="color: #008000;">&#123;</span><br />
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span> <br />
&nbsp; &nbsp; foo<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span> <span style="color: #000040;">*</span> name<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; this<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>name <span style="color: #000080;">=</span> std<span style="color: #008080;">::</span><span style="color: #007788;">string</span><span style="color: #008000;">&#40;</span>name<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #0000ff;">void</span> speak<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; std<span style="color: #008080;">::</span><span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Hello, my name is &quot;</span> <span style="color: #000080;">&lt;&lt;</span> name <span style="color: #000080;">&lt;&lt;</span> std<span style="color: #008080;">::</span><span style="color: #007788;">endl</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
<br />
<span style="color: #0000ff;">private</span><span style="color: #008080;">:</span><br />
&nbsp; &nbsp; std<span style="color: #008080;">::</span><span style="color: #007788;">string</span> name<span style="color: #008080;">;</span><br />
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span><br />
<br />
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; foo <span style="color: #000040;">*</span> fred <span style="color: #000080;">=</span> <span style="color: #0000dd;">new</span> foo<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;fred&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; fred<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>speak<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #0000dd;">delete</span> fred<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span><br />
<span style="color: #008000;">&#125;</span></div></td></tr></tbody></table></div>

<h2>Our objectives</h2>

<p>Have the output say &#8220;Hello, my name is chris&#8221; by creating a new <code>foo</code> object on the fly.</p>

<h2>Now the action</h2>

<p>Starting up&#8230;</p>

<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&gt; cl /EHsc /Zi /Fefoo.exe /Fdfoo.pdb foo.cpp<br />
&gt; windbg foo.exe</div></td></tr></tbody></table></div>

<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">0:000&gt; bm main; g</div></td></tr></tbody></table></div>

<p>F10 a few times to stop at the line <code>fred-&gt;speak()</code>.</p>

<p>The first command we&#8217;ll introduce is <code>.dvalloc</code>. We can allocate memory on the heap in the process&#8217; address space.</p>

<p>Since <code>foo::foo</code> needs a string, we&#8217;ll first create a string.</p>

<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">0:000&gt; * just put some arbitrary size<br />
0:000&gt; .dvalloc 100 <br />
Allocated 1000 bytes starting at 00150000</div></td></tr></tbody></table></div>

<p>You&#8217;ll notice that we got allocated 1000 bytes instead of 100 as we asked. That&#8217;s OK. They just like to give us whole pages.</p>

<p>Now we&#8217;ll put some string in that memory address</p>

<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">0:000&gt; eza 0x150000 &quot;chris&quot;</div></td></tr></tbody></table></div>

<p>Now we&#8217;ll call <code>foo::foo</code>. All member methods have an implicit first parameter that should be <code>this</code>. Compiler usually inject that for us, but here we need to specify it.</p>

<p>Now we&#8217;ll allocate some space for our <code>foo</code>. This time around we&#8217;ll use <code>malloc</code></p>

<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">0:000&gt; .call malloc(100); g<br />
Thread is set up for call, 'g' will execute.<br />
WARNING: This can have serious side-effects,<br />
including deadlocks and corruption of the debuggee.<br />
.call returns:<br />
void * 0x00c21310<br />
<br />
foo!main+0x6b:<br />
0126145b 8b4df0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;mov &nbsp; &nbsp; ecx,dword ptr [ebp-10h] ss:002b:0032fe2c=00c21258</div></td></tr></tbody></table></div>

<p>The thing about <code>malloc</code> is that we can get the return value nicely stored in a pseudo-register <code>$callret</code>. I usually prefer that to <code>.dvalloc</code>. Having the return value in <code>$callret</code> aids a lot in scripting WinDBG. We&#8217;ll not touch on that to not make things too difficult.</p>

<p>Now onto creating the <code>foo</code> object:</p>

<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">0:000&gt; * recall these addresses we allocated before<br />
0:000&gt;.call foo::foo(0x21310, 0x150000); g<br />
Thread is set up for call, 'g' will execute.<br />
WARNING: This can have serious side-effects,<br />
including deadlocks and corruption of the debuggee.<br />
foo!main+0x6b:<br />
0126145b 8b4df0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;mov &nbsp; &nbsp; ecx,dword ptr [ebp-10h] ss:002b:0032fe2c=00c21258</div></td></tr></tbody></table></div>

<p>Great, the final touch is to swap out <code>fred</code> with our newly created object.</p>

<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">0:000&gt;ep @@(&amp;fred) 0x21310<br />
0:000&gt;g</div></td></tr></tbody></table></div>

<p>Cool! You should get &#8220;Hello, my name is chris&#8221;</p>

<hr />

<p>If you came here searching for &#8220;WinDBG call function&#8221; or something like that, you&#8217;ve probably came across <a href="http://blogs.msdn.com/b/oldnewthing/archive/2007/04/27/2292037.aspx">this post</a> on The Old New Thing. That one is more advanced than what we have here <img src='http://cfc.kizzx2.com/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' />  Probably this will serve as a gentler introduction.</p>
]]></content:encoded>
			<wfw:commentRss>http://cfc.kizzx2.com/index.php/tutorial-using-windbg-to-call-arbitrary-functions-windbg-kung-fu-series/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Detecting memory leaks with WinDBG the modern (and free) way</title>
		<link>http://cfc.kizzx2.com/index.php/detecting-memory-leaks-with-windbg-the-modern-and-free-way/</link>
		<comments>http://cfc.kizzx2.com/index.php/detecting-memory-leaks-with-windbg-the-modern-and-free-way/#comments</comments>
		<pubDate>Fri, 08 Oct 2010 16:36:40 +0000</pubDate>
		<dc:creator>kizzx2</dc:creator>
				<category><![CDATA[Useful tips]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[crtdbg]]></category>
		<category><![CDATA[memory leaks]]></category>
		<category><![CDATA[windbg]]></category>
		<category><![CDATA[windows]]></category>

		<guid isPermaLink="false">http://cfc.kizzx2.com/?p=215</guid>
		<description><![CDATA[This information is surprisingly scarce on the Internet. I should have realized earlier that the world has simply moved on to more modern languages than those who tax us to deal with mundane things like memory leaks. Anyway, for those who&#8217;re still stuck, this should be your savior. This is almost as good as Valgrind [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="margin-left: 10px; float: right;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fcfc.kizzx2.com%2Findex.php%2Fdetecting-memory-leaks-with-windbg-the-modern-and-free-way%2F">
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fcfc.kizzx2.com%2Findex.php%2Fdetecting-memory-leaks-with-windbg-the-modern-and-free-way%2F&amp;source=kizzx2&amp;style=normal&amp;b=2" height="61" width="50" />
			</a>
		</div><p>This information is surprisingly scarce on the Internet. I should have realized earlier that the world has simply moved on to more modern languages than those who tax us to deal with mundane things like memory leaks.</p>

<p>Anyway, for those who&#8217;re still stuck, this should be your savior. This is almost as good as Valgrind on nix</p>

<h3>Minimal example</h3>

<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br /></div></td><td><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; <span style="color: #339900;">#include &lt;crtdbg.h&gt;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #0000ff;">void</span> leak<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080;">::</span><span style="color: #007788;">operator</span> <span style="color: #0000dd;">new</span><span style="color: #008000;">&#40;</span><span style="color: #0000dd;">1000</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000dd;">new</span> <span style="color: #0000ff;">double</span><span style="color: #008000;">&#91;</span><span style="color: #0000dd;">1000</span><span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; _CrtSetDbgFlag<span style="color: #008000;">&#40;</span>_CRTDBG_ALLOC_MEM_DF <span style="color: #000040;">|</span> _CRTDBG_LEAK_CHECK_DF<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; leak<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span></div></td></tr></tbody></table></div>

<p>Running the above code will get you something like this in the Debug output:</p>

<pre><code>Detected memory leaks!
Dumping objects -&gt;
{67} normal block at 0x00573FC8, 8000 bytes long.
 Data: &lt;                &gt; CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD 
{66} normal block at 0x005713E8, 1000 bytes long.
 Data: &lt;                &gt; CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD 
Object dump complete.
</code></pre>

<p>This is nice and good, but it isn&#8217;t very useful since we (pretend) don&#8217;t know where the leak came from!</p>

<h3>And then WinDBG comes in</h3>

<p>(Obviously, add WinDBG to your <code>PATH</code> first)</p>

<p>First, we&#8217;ll turn on heap stack tracing for our program. <code>gflags.exe</code> needs to be run with elevated privileges:</p>

<div class="codecolorer-container dos default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="dos codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; <span style="color: #33cc33;">&gt;</span> gflags /i leak.exe +ust</div></td></tr></tbody></table></div>

<p>Now we&#8217;re ready to go!</p>

<div class="codecolorer-container dos default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="dos codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; <span style="color: #33cc33;">&gt;</span> windbg -g leak.exe</div></td></tr></tbody></table></div>

<p>At the debug output in WinDBG, we&#8217;ll see our leak report:</p>

<pre><code>Detected memory leaks!
Dumping objects -&gt;
{80} normal block at 0x003B4378, 8000 bytes long.
 Data: &lt;                &gt; CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD 
{79} normal block at 0x003B3F50, 1000 bytes long.
 Data: &lt;                &gt; CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD 
Object dump complete.
</code></pre>

<p>At WinDBG, we can trace down the originating functions</p>

<pre><code>000:0&gt; !heap -p -a 0x003B4378

address 004b4378 found in
_HEAP @ 4b0000
  HEAP_ENTRY Size Prev Flags    UserPtr UserSize - state
    004b4340 03f0 0000  [00]   004b4358    01f64 - (busy)
    Trace: cfba8
    7753dd4d ntdll!RtlAllocateHeap+0x00000274
    6fa87743 MSVCR100D!_heap_alloc_base+0x00000053
    6fa95d8c MSVCR100D!_heap_alloc_dbg_impl+0x000001fc
    6fa95b2f MSVCR100D!_nh_malloc_dbg_impl+0x0000001f
    6fa95adc MSVCR100D!_nh_malloc_dbg+0x0000002c
    6fa9906b MSVCR100D!malloc+0x0000001b
    6fa871b1 MSVCR100D!operator new+0x00000011
    6fa872ef MSVCR100D!operator new[]+0x0000000f
</code></pre>

<p><strong><pre style="background-color: InfoBackground">    dc13d5 leak!leak+0x00000035</pre></strong></p>

<pre><code>    dc1447 leak!main+0x00000037
    dc19df leak!__tmainCRTStartup+0x000001bf
    dc180f leak!mainCRTStartup+0x0000000f
    75f43677 kernel32!BaseThreadInitThunk+0x0000000e
    774f9d42 ntdll!__RtlUserThreadStart+0x00000070
    774f9d15 ntdll!_RtlUserThreadStart+0x0000001b 
</code></pre>

<p>There you go! The highlighted line above is the culprit. For those unfamiliar with WinDBG, here&#8217;s a little more hand holding: Click on the source code view, <code>Ctrl+G</code> and then type <code>leak+0x35</code>.</p>

<p>Remember to turn gflags off afterwards. The gflags is a registry and won&#8217;t clear even if you delete the file itself.</p>

<div class="codecolorer-container dos default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="dos codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; <span style="color: #33cc33;">&gt;</span> gflags /i leak.exe -ust</div></td></tr></tbody></table></div>

<h3>32/64 bit</h3>

<p>To debug 32 bit applications in a 64 bit system, you need to use the 32 bit version of WinDBG, which is not installed by default. You can find <code>dbg_x86_*.msi</code> somewhere in your &#8220;Microsoft SDKs&#8221; installation directory.</p>

<h3>More advanced usage</h3>

<p>Heap debugging is not restricted to detecting leaks. If you find your application allocating a lot of memory in some places, you can easily trace down where they come from with WinDBG and the <code>!heap</code> extension. <a href="http://www.codeproject.com/KB/cpp/MemoryLeak.aspx">This CodeProject article</a> describes how to do just that in depth.</p>

<p>However, for doing that, I find a more modern and less intrusive way to be using <a href="http://msdn.microsoft.com/en-us/performance/default.aspx">XPerf</a> tool. That combined with WinDBG and you&#8217;ve got a debugging power-house. (Resources on how to do it is extremely scarce. If you thought this WinDBG technique is obscure, you ain&#8217;t seen nothing :p I may write an post about that some time later when I feel like it.)</p>

<h3>Wrong way of doing it</h3>

<p><a href="http://msdn.microsoft.com/en-us/library/e5ewb1h3(VS.80).aspx">MSDN</a> suggests something like this</p>

<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br /></div></td><td><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; <span style="color: #339900;">#define _CRTDBG_MAP_ALLOC</span><br />
&nbsp; &nbsp; <span style="color: #339900;">#include &lt;stdlib.h&gt;</span><br />
&nbsp; &nbsp; <span style="color: #339900;">#include &lt;crtdbg.h&gt;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #666666;">// At the end of your program</span><br />
&nbsp; &nbsp; _CrtDumpMemoryLeaks<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></div></td></tr></tbody></table></div>

<p>This solution is inadequate for a couple of reasons:</p>

<ul>
<li>It only lists source code location for <code>malloc</code>. What&#8217;s the point?</li>
<li>Calling <code>_CrtDumpMemoryLeaks()</code> at the end of <code>main()</code> would mistakenly report memory allocated by static objects as leaks.</li>
</ul>

<p>The only good thing about it is that you don&#8217;t need to leave your comfort zone of Visual Studio (that&#8217;s probably the entire programmer life for many people, unfortunately).</p>

<h3>Some other good resources</h3>

<ul>
<li><a href="http://msdn.microsoft.com/en-us/library/e5ewb1h3(VS.80).aspx">Memory Leak Detection Enabling</a></li>
<li><a href="http://windbg.info/doc/1-common-cmds.html">Common WinDbg Commands (Thematically Grouped)</a></li>
<li><a href="http://hacksoflife.blogspot.com/2009/06/heap-debugging-memoryresource-leak-with.html">The Hacks of Life: Heap Debugging (Memory/Resource Leak with WinDbg)</a></li>
<li><a href="http://www.codeproject.com/KB/cpp/MemoryLeak.aspx">Memory Leak Detection Using Windbg &#8211; CodeProject</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://cfc.kizzx2.com/index.php/detecting-memory-leaks-with-windbg-the-modern-and-free-way/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Cygwin slow start up: the culprit discovered!</title>
		<link>http://cfc.kizzx2.com/index.php/cygwin-slow-start-up-the-culprit-discovered/</link>
		<comments>http://cfc.kizzx2.com/index.php/cygwin-slow-start-up-the-culprit-discovered/#comments</comments>
		<pubDate>Tue, 07 Sep 2010 15:33:19 +0000</pubDate>
		<dc:creator>kizzx2</dc:creator>
				<category><![CDATA[Useful tips]]></category>
		<category><![CDATA[cygwin slow performance bash-completion]]></category>

		<guid isPermaLink="false">http://cfc.kizzx2.com/?p=209</guid>
		<description><![CDATA[If you&#8217;ve found the start-up time of Cygwin slowing down significantly after you&#8217;ve upgraded from 1.5 to 1.7, here&#8217;s a one liner to improve it around 500%: 1$ mv /etc/profile.d/bash_completion.sh&#123;,.disabled&#125; The bash_completion is apparently undergoing some massive restructure. The fact that fork is very inefficient on Windows (Windows&#8217; inferior process model can&#8217;t really handle that, [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="margin-left: 10px; float: right;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fcfc.kizzx2.com%2Findex.php%2Fcygwin-slow-start-up-the-culprit-discovered%2F">
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fcfc.kizzx2.com%2Findex.php%2Fcygwin-slow-start-up-the-culprit-discovered%2F&amp;source=kizzx2&amp;style=normal&amp;b=2" height="61" width="50" />
			</a>
		</div><p>If you&#8217;ve found the start-up time of Cygwin slowing down significantly after you&#8217;ve upgraded from 1.5 to 1.7, here&#8217;s a one liner to improve it around 500%:</p>

<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666;">$ </span><span style="color: #c20cb9; font-weight: bold;">mv</span> <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>profile.d<span style="color: #000000; font-weight: bold;">/</span>bash_completion.sh<span style="color: #7a0874; font-weight: bold;">&#123;</span>,.disabled<span style="color: #7a0874; font-weight: bold;">&#125;</span></div></td></tr></tbody></table></div>

<p>The <code>bash_completion</code> is apparently undergoing some massive restructure. The fact that <code>fork</code> is very inefficient on Windows (Windows&#8217; inferior process model can&#8217;t really handle that, Cygwin did a great job mimicking it) make it not really ready for prime time on Cygwin.</p>

<p>You can still get <code>bash_completion</code> manually if you need it:</p>

<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666;">$ </span>. <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>bash_completion</div></td></tr></tbody></table></div>
]]></content:encoded>
			<wfw:commentRss>http://cfc.kizzx2.com/index.php/cygwin-slow-start-up-the-culprit-discovered/feed/</wfw:commentRss>
		<slash:comments>17</slash:comments>
		</item>
		<item>
		<title>Iterating Bash arrays with spaces</title>
		<link>http://cfc.kizzx2.com/index.php/iterating-bash-arrays-with-spaces/</link>
		<comments>http://cfc.kizzx2.com/index.php/iterating-bash-arrays-with-spaces/#comments</comments>
		<pubDate>Fri, 09 Apr 2010 17:27:35 +0000</pubDate>
		<dc:creator>kizzx2</dc:creator>
				<category><![CDATA[Useful tips]]></category>
		<category><![CDATA[arrays]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[for loop]]></category>
		<category><![CDATA[iteration]]></category>

		<guid isPermaLink="false">http://cfc.kizzx2.com/?p=123</guid>
		<description><![CDATA[The problem 1234567891011121314151617181920212223242526272829#!/bin/sh $ a=&#40;hello world foo bar&#41; $ for i in #{a[*]}; do echo $i; done # Expected output: # &#160; hello # &#160; world # &#160; foo # &#160; bar # so far so good: # &#160; hello # &#160; world # &#160; foo # &#160; bar $ a=&#40;&#34;hello world&#34; &#34;foo bar&#34;&#41; $ [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="margin-left: 10px; float: right;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fcfc.kizzx2.com%2Findex.php%2Fiterating-bash-arrays-with-spaces%2F">
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fcfc.kizzx2.com%2Findex.php%2Fiterating-bash-arrays-with-spaces%2F&amp;source=kizzx2&amp;style=normal&amp;b=2" height="61" width="50" />
			</a>
		</div><h2>The problem</h2>

<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br /></div></td><td><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;">#!/bin/sh</span><br />
<br />
$ <span style="color: #007800;">a</span>=<span style="color: #7a0874; font-weight: bold;">&#40;</span>hello world foo bar<span style="color: #7a0874; font-weight: bold;">&#41;</span><br />
$ <span style="color: #000000; font-weight: bold;">for</span> i <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #666666; font-style: italic;">#{a[*]}; do echo $i; done</span><br />
<br />
<span style="color: #666666; font-style: italic;"># Expected output:</span><br />
<span style="color: #666666; font-style: italic;"># &nbsp; hello</span><br />
<span style="color: #666666; font-style: italic;"># &nbsp; world</span><br />
<span style="color: #666666; font-style: italic;"># &nbsp; foo</span><br />
<span style="color: #666666; font-style: italic;"># &nbsp; bar</span><br />
<br />
<span style="color: #666666; font-style: italic;"># so far so good:</span><br />
<span style="color: #666666; font-style: italic;"># &nbsp; hello</span><br />
<span style="color: #666666; font-style: italic;"># &nbsp; world</span><br />
<span style="color: #666666; font-style: italic;"># &nbsp; foo</span><br />
<span style="color: #666666; font-style: italic;"># &nbsp; bar</span><br />
<br />
$ <span style="color: #007800;">a</span>=<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #ff0000;">&quot;hello world&quot;</span> <span style="color: #ff0000;">&quot;foo bar&quot;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span><br />
$ <span style="color: #000000; font-weight: bold;">for</span> i <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #666666; font-style: italic;">#{a[*]}; do echo $i; done</span><br />
<br />
<span style="color: #666666; font-style: italic;"># Expected output:</span><br />
<span style="color: #666666; font-style: italic;"># &nbsp; hello world</span><br />
<span style="color: #666666; font-style: italic;"># &nbsp; foo bar</span><br />
<br />
<span style="color: #666666; font-style: italic;"># omg:</span><br />
<span style="color: #666666; font-style: italic;"># &nbsp; hello</span><br />
<span style="color: #666666; font-style: italic;"># &nbsp; world</span><br />
<span style="color: #666666; font-style: italic;"># &nbsp; foo</span><br />
<span style="color: #666666; font-style: italic;"># &nbsp; bar</span></div></td></tr></tbody></table></div>

<p>The problem is caused by the affect that Bash uses space as array element separator internally.</p>

<h2>Failed attempt</h2>

<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br /></div></td><td><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;">#!/bin/sh</span><br />
<br />
$ <span style="color: #007800;">a</span>=<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #ff0000;">&quot;hello world&quot;</span> <span style="color: #ff0000;">&quot;foo bar&quot;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span><br />
$ <span style="color: #000000; font-weight: bold;">for</span> i <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #ff0000;">&quot;#{a[*]}&quot;</span>; <span style="color: #000000; font-weight: bold;">do</span> <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #007800;">$i</span>; <span style="color: #000000; font-weight: bold;">done</span><br />
<br />
<span style="color: #666666; font-style: italic;"># Expected output:</span><br />
<span style="color: #666666; font-style: italic;"># &nbsp; hello world</span><br />
<span style="color: #666666; font-style: italic;"># &nbsp; foo bar</span><br />
<br />
<span style="color: #666666; font-style: italic;"># zomg!</span><br />
<span style="color: #666666; font-style: italic;"># &nbsp; hello world foo bar</span></div></td></tr></tbody></table></div>

<h2>The solution</h2>

<p>The solution lies in the magical <code>$@</code> expansion. When the <code>$@</code> expansion is put in a quote, the shell automatically expands each element properly quoted:</p>

<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br /></div></td><td><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;">#!/bin/sh</span><br />
<br />
<span style="color: #007800;">a</span>=<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #ff0000;">&quot;hello world&quot;</span> <span style="color: #ff0000;">&quot;foo bar&quot;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span><br />
<span style="color: #000000; font-weight: bold;">for</span> i <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #ff0000;">&quot;#{a[@]}&quot;</span>; <span style="color: #000000; font-weight: bold;">do</span> <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #007800;">$i</span>; <span style="color: #000000; font-weight: bold;">done</span><br />
<br />
<span style="color: #666666; font-style: italic;"># Expected output:</span><br />
<span style="color: #666666; font-style: italic;"># &nbsp; hello world</span><br />
<span style="color: #666666; font-style: italic;"># &nbsp; foo bar</span><br />
<br />
<span style="color: #666666; font-style: italic;"># yay!</span><br />
<span style="color: #666666; font-style: italic;"># &nbsp; hello world</span><br />
<span style="color: #666666; font-style: italic;"># &nbsp; foo bar</span></div></td></tr></tbody></table></div>
]]></content:encoded>
			<wfw:commentRss>http://cfc.kizzx2.com/index.php/iterating-bash-arrays-with-spaces/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Muxing audio and video with ffmpeg</title>
		<link>http://cfc.kizzx2.com/index.php/muxing-audio-and-video-with-ffmpeg/</link>
		<comments>http://cfc.kizzx2.com/index.php/muxing-audio-and-video-with-ffmpeg/#comments</comments>
		<pubDate>Thu, 08 Apr 2010 10:10:20 +0000</pubDate>
		<dc:creator>kizzx2</dc:creator>
				<category><![CDATA[Useful tips]]></category>
		<category><![CDATA[ffmpeg]]></category>
		<category><![CDATA[multiplexing]]></category>
		<category><![CDATA[mux]]></category>

		<guid isPermaLink="false">http://cfc.kizzx2.com/?p=112</guid>
		<description><![CDATA[ffmpeg is a great codec converter, but it&#8217;s wide array of options is really daunting. I just figured out it can also be used to mux video and audio together into a file, here&#8217;s how to do it 123456# Simple example: mux an audio with a video file without audio track $ ffmpeg -i audio.mp3 [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="margin-left: 10px; float: right;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fcfc.kizzx2.com%2Findex.php%2Fmuxing-audio-and-video-with-ffmpeg%2F">
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fcfc.kizzx2.com%2Findex.php%2Fmuxing-audio-and-video-with-ffmpeg%2F&amp;source=kizzx2&amp;style=normal&amp;b=2" height="61" width="50" />
			</a>
		</div><p>ffmpeg is a great codec converter, but it&#8217;s wide array of options is really daunting. I just figured out it can also be used to mux video and audio together into a file, here&#8217;s how to do it</p>

<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br /></div></td><td><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;"># Simple example: mux an audio with a video file without audio track</span><br />
$ <span style="color: #c20cb9; font-weight: bold;">ffmpeg</span> <span style="color: #660033;">-i</span> audio.mp3 <span style="color: #660033;">-i</span> video.avi <span style="color: #660033;">-acodec</span> copy <span style="color: #660033;">-vcodec</span> copy output.avi<br />
<br />
<span style="color: #666666; font-style: italic;"># Daily usage example: mux an audio with a video file _with_ an existing audio track.</span><br />
<span style="color: #666666; font-style: italic;"># This will replace the AVI file's audio track with the MP3</span><br />
$ <span style="color: #c20cb9; font-weight: bold;">ffmpeg</span> <span style="color: #660033;">-i</span> audio.mp3 <span style="color: #660033;">-i</span> video-with-audio.avi <span style="color: #660033;">-acodec</span> copy <span style="color: #660033;">-vcodec</span> copy output.avi <span style="color: #660033;">-map</span> <span style="color: #000000;">0.0</span> <span style="color: #660033;">-map</span> <span style="color: #000000;">1.0</span></div></td></tr></tbody></table></div>

<p>The key to the second command is the <code>-map</code> parameter. Typically, the output file would contain two streams: one audio and one video. The numbers <code>0.0</code> and <code>1.0</code> refers to the first input file and the second input file respectively.</p>

<p>The <code>-map</code> parameter is used to <em>spell out</em> the streams. What the above said was &#8220;use input stream 0.0 for your first output stream (which is an audio stream) and input stream 1.0 for your second output stream (which is the video stream)&#8221;.</p>

<p>You can see a list of stream numbers by</p>

<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666;">$ </span><span style="color: #c20cb9; font-weight: bold;">ffmpeg</span> <span style="color: #660033;">-i</span> audio.mp3 <span style="color: #660033;">-i</span> video-with-audio.avi</div></td></tr></tbody></table></div>

<p>You can add <em>additional</em> streams to the output file with the <code>-newaudio</code> and <code>-newvideo</code> parameters. Two audio streams make sense when you&#8217;re making a DVD rip with two sound tracks. I didn&#8217;t try it myself but it&#8217;s nice to know <img src='http://cfc.kizzx2.com/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /> </p>

<p>Cheers <img src='http://cfc.kizzx2.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://cfc.kizzx2.com/index.php/muxing-audio-and-video-with-ffmpeg/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Vrapper: open source vim plugin for eclipse</title>
		<link>http://cfc.kizzx2.com/index.php/vrapper-open-source-vim-plugin-for-eclipse/</link>
		<comments>http://cfc.kizzx2.com/index.php/vrapper-open-source-vim-plugin-for-eclipse/#comments</comments>
		<pubDate>Thu, 08 Apr 2010 09:49:12 +0000</pubDate>
		<dc:creator>kizzx2</dc:creator>
				<category><![CDATA[Useful tips]]></category>
		<category><![CDATA[eclipse]]></category>
		<category><![CDATA[jvi]]></category>
		<category><![CDATA[vi]]></category>
		<category><![CDATA[vim]]></category>
		<category><![CDATA[viplugin]]></category>
		<category><![CDATA[vrapper]]></category>

		<guid isPermaLink="false">http://cfc.kizzx2.com/?p=110</guid>
		<description><![CDATA[Found out Vrapper recently. The fact that it is open source deserves a lot of merits. For years eclipse users only had the commercial option ViPlugin which didn&#8217;t work quite well for something that charges money, considering the NetBeans folks have the excellent jVi which is also open source. Considering eclipse is free and open [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="margin-left: 10px; float: right;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fcfc.kizzx2.com%2Findex.php%2Fvrapper-open-source-vim-plugin-for-eclipse%2F">
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fcfc.kizzx2.com%2Findex.php%2Fvrapper-open-source-vim-plugin-for-eclipse%2F&amp;source=kizzx2&amp;style=normal&amp;b=2" height="61" width="50" />
			</a>
		</div><p>Found out <a href="http://vrapper.sourceforge.net/">Vrapper</a> recently. The fact that it is open source deserves a lot of merits. For years eclipse users only had the commercial option <a href="http://www.viplugin.com/">ViPlugin</a> which didn&#8217;t work quite well for something that charges money, considering the NetBeans folks have the excellent <a href="http://jvi.sourceforge.net/">jVi</a> which is also open source.</p>

<p>Considering eclipse is free and open source, Vrapper only seems more fitting than ViPlugin. I&#8217;ve tried it for a week and it seems most essential functions are there, and I&#8217;ve been using vi for a couple of years so I think my set of &#8220;essential functions&#8221; shouldn&#8217;t be too narrow :p</p>
]]></content:encoded>
			<wfw:commentRss>http://cfc.kizzx2.com/index.php/vrapper-open-source-vim-plugin-for-eclipse/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Installing HuffYUV on Windows 7 x64</title>
		<link>http://cfc.kizzx2.com/index.php/installing-huffyuv-on-windows-7-x64/</link>
		<comments>http://cfc.kizzx2.com/index.php/installing-huffyuv-on-windows-7-x64/#comments</comments>
		<pubDate>Mon, 05 Apr 2010 10:18:40 +0000</pubDate>
		<dc:creator>kizzx2</dc:creator>
				<category><![CDATA[Useful tips]]></category>
		<category><![CDATA[64 bit]]></category>
		<category><![CDATA[huffyuv]]></category>
		<category><![CDATA[install inf]]></category>
		<category><![CDATA[lagarith]]></category>
		<category><![CDATA[x64]]></category>

		<guid isPermaLink="false">http://cfc.kizzx2.com/?p=99</guid>
		<description><![CDATA[This will probably also apply to other INF based installation for that matter. A simple command line saved me hours of trouble: rundll32 C:\Windows\SysWOW64\setupapi.dll,InstallHinfSection DefaultInstall 0 C:\Path\To\huffyuv.inf Run the above with admin privilege and bam, HuffYUV correctly appears in my codec list. P.S. If you&#8217;re looking for a lossless codec, maybe take a look at [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="margin-left: 10px; float: right;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fcfc.kizzx2.com%2Findex.php%2Finstalling-huffyuv-on-windows-7-x64%2F">
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fcfc.kizzx2.com%2Findex.php%2Finstalling-huffyuv-on-windows-7-x64%2F&amp;source=kizzx2&amp;style=normal&amp;b=2" height="61" width="50" />
			</a>
		</div><p>This will probably also apply to other INF based installation for that matter. A simple command line saved me hours of trouble:</p>

<pre><code>rundll32 C:\Windows\SysWOW64\setupapi.dll,InstallHinfSection DefaultInstall 0 C:\Path\To\huffyuv.inf
</code></pre>

<p>Run the above with admin privilege and bam, HuffYUV correctly appears in my codec list.</p>

<p>P.S. If you&#8217;re looking for a lossless codec, maybe take a look at <a href="http://lags.leetcode.net/codec.html">Lagarith</a>. It claims to be an improvement to HuffYUV and supports multi-processor encoding. It also comes with a dedicated installer so there&#8217;s no need to tinker with the command line just to install the codec.</p>
]]></content:encoded>
			<wfw:commentRss>http://cfc.kizzx2.com/index.php/installing-huffyuv-on-windows-7-x64/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>cmd-recycle: Delete files from Windows command line</title>
		<link>http://cfc.kizzx2.com/index.php/cmd-recycle-delete-files-from-windows-command-line/</link>
		<comments>http://cfc.kizzx2.com/index.php/cmd-recycle-delete-files-from-windows-command-line/#comments</comments>
		<pubDate>Tue, 05 Jan 2010 19:30:48 +0000</pubDate>
		<dc:creator>kizzx2</dc:creator>
				<category><![CDATA[Useful tips]]></category>
		<category><![CDATA[command-line]]></category>
		<category><![CDATA[recycle-bin]]></category>
		<category><![CDATA[windows]]></category>

		<guid isPermaLink="false">http://cfc.kizzx2.com/?p=78</guid>
		<description><![CDATA[Download links Direct download browse on GitHub I wrote another tool to do just this a while ago, but that one stopped working since I migrated to Windows 7 x64. Microsoft said the SHFileOpearation interface (which the old tool used) had been replaced by the IFileOperation interface. So I figured I would dig up my [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="margin-left: 10px; float: right;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fcfc.kizzx2.com%2Findex.php%2Fcmd-recycle-delete-files-from-windows-command-line%2F">
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fcfc.kizzx2.com%2Findex.php%2Fcmd-recycle-delete-files-from-windows-command-line%2F&amp;source=kizzx2&amp;style=normal&amp;b=2" height="61" width="50" />
			</a>
		</div><h2>Download links</h2>

<ul>
<li><a href="http://cloud.github.com/downloads/kizzx2/cmd-recycle/recycle.exe"><strong>Direct download</strong></a></li>
<li><a href="http://github.com/kizzx2/cmd-recycle">browse on GitHub</a></li>
</ul>

<p>I wrote <a href="http://kizzx2.com/blog/index.php/2008/09/27/windows-delete-to-recycle-bin-from-the-command-line/">another tool</a> to do just this a while ago, but that one stopped working since I migrated to Windows 7 x64. Microsoft said the <code>SHFileOpearation</code> interface (which the old tool used) <a href="http://msdn.microsoft.com/en-us/library/bb775771(VS.85).aspx">had been replaced by the <code>IFileOperation</code> interface</a>. So I figured I would dig up my rusty C# again to update it.</p>

<p>Damn, I can&#8217;t believe it took a couple of hours, but the result worked amazingly. This one also supports wildcards:</p>

<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">recycle file1 file2 supports-wildcards\*.tmp</div></td></tr></tbody></table></div>

<p>I&#8217;ve gone the extra mile and set up a proper repository this time around <a href="http://github.com/kizzx2/cmd-recycle">on GitHub</a>. Probably some Windows gurus will mock me left and right with some obscure one-liners or something, lol.</p>
]]></content:encoded>
			<wfw:commentRss>http://cfc.kizzx2.com/index.php/cmd-recycle-delete-files-from-windows-command-line/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Starting Windows&#8217; Network and Sharing Center from command prompt</title>
		<link>http://cfc.kizzx2.com/index.php/starting-windows-network-and-sharing-center-from-command-prompt/</link>
		<comments>http://cfc.kizzx2.com/index.php/starting-windows-network-and-sharing-center-from-command-prompt/#comments</comments>
		<pubDate>Tue, 29 Dec 2009 13:21:46 +0000</pubDate>
		<dc:creator>kizzx2</dc:creator>
				<category><![CDATA[Useful tips]]></category>
		<category><![CDATA[command-line]]></category>
		<category><![CDATA[network-and-sharing-center]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[windows]]></category>

		<guid isPermaLink="false">http://cfc.kizzx2.com/?p=76</guid>
		<description><![CDATA[It has always looked not-so-cool to have to type &#8220;Sharing&#8221; at the start menu or worse yet, navigate through the Control Panel just to start it. Today I finally dug out how to do it from TechNet: 1control /name Microsoft.NetworkAndSharingCenter Looks like Microsoft decided to go down the verbose path. Maybe I&#8217;ll just stick with [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="margin-left: 10px; float: right;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fcfc.kizzx2.com%2Findex.php%2Fstarting-windows-network-and-sharing-center-from-command-prompt%2F">
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fcfc.kizzx2.com%2Findex.php%2Fstarting-windows-network-and-sharing-center-from-command-prompt%2F&amp;source=kizzx2&amp;style=normal&amp;b=2" height="61" width="50" />
			</a>
		</div><p>It has always looked not-so-cool to have to type &#8220;Sharing&#8221; at the start menu or worse yet, navigate through the Control Panel just to start it. Today I finally dug out how to do it from <a href="http://technet.microsoft.com/en-us/library/cc733147(WS.10).aspx">TechNet</a>:</p>

<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">control /name Microsoft.NetworkAndSharingCenter</div></td></tr></tbody></table></div>

<p>Looks like Microsoft decided to go down the verbose path. Maybe I&#8217;ll just stick with the old ways :p</p>
]]></content:encoded>
			<wfw:commentRss>http://cfc.kizzx2.com/index.php/starting-windows-network-and-sharing-center-from-command-prompt/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

