<?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>pangwa&#039;s Blog &#187; MFC</title>
	<atom:link href="http://blog.pangwa.com/tag/mfc/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.pangwa.com</link>
	<description>Always be pangwa</description>
	<lastBuildDate>Sat, 06 Aug 2011 15:21:04 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
	<atom:link rel='hub' href='http://blog.pangwa.com/?pushpress=hub'/>
		<item>
		<title>闪屏问题</title>
		<link>http://blog.pangwa.com/2009/04/22/flashing-bomb/</link>
		<comments>http://blog.pangwa.com/2009/04/22/flashing-bomb/#comments</comments>
		<pubDate>Wed, 22 Apr 2009 13:09:20 +0000</pubDate>
		<dc:creator>pangwa</dc:creator>
				<category><![CDATA[Tech]]></category>
		<category><![CDATA[MFC]]></category>

		<guid isPermaLink="false">http://blog.pangwa.com/2009/04/22/flashing-bomb/</guid>
		<description><![CDATA[今天又接到了一个问题:-&#124; 最近的问题有点多哈! 问题是这样的, 朋友的一个程序(VC++编写)它会在上面显示一个背景图片和自己绘制的一个计时的时间显示表, 程序在开发的阶段运行一切正常, 但是客户反映讲此程序运行的相当不稳定, 总是在过了一段时间后会出现背景变灰, 并且窗口界面开始不停的闪烁. 朋友经过测试也发现有类似的状况, 在朋友的机器上程序大概运行三分钟后就会出现客户说的情况, 据朋友描述说有时也两分钟左右就会出现闪烁的情况, 只是这种情况比较少, 但三分钟是一个比较保险的使问题重现的时间. 刚听到这个问题, 比较没有头绪, 找不到问题, 于是让朋友把相关的代码发过来, 看了代码心中大概有了一个概念: 界面完全是由自己的代码绘制的, 为了显示计时器的时间, 在代码里加了一个计时器, 并在onTimer ()时重新绘制整个窗口 绘制时钟是通过装载对应的位图绘制数字的 看到这里, 心里大概能猜测到了一个可能的原因: 句柄泄漏, 一想到这一点, 所有的问题都可以解释了: 极有可能是在窗口绘制的代码里出现了句柄泄漏的情况, 在刚开始的时候没有问题, 但由于每秒钟都会定时绘制一下界面, 所以随着时间的推移, 泄漏的句柄数越来越多 &#8212; 这样当所有可用的句柄都泄漏完之后, 当再次尝试申请句柄资源以创建相关的位图对象的时候就会失败, 这样就会导致画不出相关的图片来, 于是整个界面就会是灰/白色了, 至于闪烁的问题, 应该是由于每次刷新失败导致的问题. 这么一讲还真可以讲的通. 再仔细看一下代码吧:) 下面是一段类似于朋友ontimer函数的代码: void CMyDlg::OnTimer(UINT nIDEvent) { if (nIDEvent == MY_TIMER_EVENT) { // // [...]]]></description>
			<content:encoded><![CDATA[<p>今天又接到了一个问题:-| 最近的问题有点多哈! </p>
<p>问题是这样的, 朋友的一个程序(VC++编写)它会在上面显示一个背景图片和自己绘制的一个计时的时间显示表, 程序在开发的阶段运行一切正常, 但是客户反映讲此程序运行的相当不稳定, 总是在过了一段时间后会出现背景变灰, 并且窗口界面开始不停的闪烁. 朋友经过测试也发现有类似的状况, 在朋友的机器上程序大概运行三分钟后就会出现客户说的情况, 据朋友描述说有时也两分钟左右就会出现闪烁的情况, 只是这种情况比较少, 但三分钟是一个比较保险的使问题重现的时间. </p>
<p>刚听到这个问题, 比较没有头绪, 找不到问题, 于是让朋友把相关的代码发过来, 看了代码心中大概有了一个概念:</p>
<ol>
<li>界面完全是由自己的代码绘制的, </li>
<li>为了显示计时器的时间, 在代码里加了一个计时器, 并在onTimer ()时重新绘制整个窗口 </li>
<li>绘制时钟是通过装载对应的位图绘制数字的 </li>
</ol>
<p>看到这里, 心里大概能猜测到了一个可能的原因: 句柄泄漏, 一想到这一点, 所有的问题都可以解释了: 极有可能是在窗口绘制的代码里出现了句柄泄漏的情况, 在刚开始的时候没有问题, 但由于每秒钟都会定时绘制一下界面, 所以随着时间的推移, 泄漏的句柄数越来越多 &#8212; 这样当所有可用的句柄都泄漏完之后, 当再次尝试申请句柄资源以创建相关的位图对象的时候就会失败, 这样就会导致画不出相关的图片来, 于是整个界面就会是灰/白色了, 至于闪烁的问题, 应该是由于每次刷新失败导致的问题. 这么一讲还真可以讲的通. 再仔细看一下代码吧:) 下面是一段类似于朋友ontimer函数的代码:</p>
<pre lang="cpp">
void CMyDlg::OnTimer(UINT nIDEvent)
{
    if (nIDEvent == MY_TIMER_EVENT)
    {
        //
        // draw the clock
        //
         CDC *pDC = GetDC();
        CDC CompatibleDC;
        CompatibleDC.CreateCompatibleDC(pDC);
        CBitmap bmp;
        bmp.LoadBitmap(IDB_DIGITS);
        BITMAP bitmap;
        bmp.GetBitmap(&amp;bitmap);
        CompatibleDC.SelectObject(&amp;bmp);
        //
        // draw minutes/seconds, using hte digits in the bitmap...
        //
        pDC-&gt;StretchBlt(200,565,87,135, &amp;CompatibleDC,(bitmap.bmWidth/12) * minute,
                0, bitmap.bmWidth/12,bitmap.bmHeight,SRCCOPY);

        ReleaseDC(pDC);
    }
}</pre>
<p>初看貌似没有什么问题, dc对象被正常释放了, 不过再仔细看一下! 注意这里的CBitmap以及selectObject, 这里是有泄漏的,或许你会说CBitmap的析构函数会释放它使用的gdi 对象, 在msdn里的LoadBitmap的方法说明里也是这么讲的:</p>
<blockquote>
<p>The loaded bitmap is attached to the CBitmap object. </p>
<p>If the bitmap identified by lpszResourceName does not exist or if there is insufficient memory to load the bitmap, the function returns 0. </p>
<p>You can use the CGdiObject::DeleteObject function to delete bitmap loaded by the LoadBitmap function, or the CBitmap destructor will delete the object for you. </p>
</blockquote>
<p>但是请注意下面的一行!</p>
<blockquote>
<p><font color="#ff0000">Caution:<br />
      <br />Before you delete the object, make sure it is not selected into a device context. </font></p>
</blockquote>
<p>这里提到请注意在删除一个对象之前请确认它没还没当前的设备上下文选中! 如果删除一个当前被使用的对象会有什么后果? 虽然没有找到文档, 但照猜测的话那就是删除失败! 所以毫无疑问每次都会导致一个CBitmap的gdi对象的泄漏! 所以修好这个问题应该是在使用完这个bitmap对象后再使用selectObject把选择原来的gdi 对象, 原来的gdi对象可以通过CompatibleDC.SelectObject(&amp;bmp);的返回值得到. 所以修改后的代码应该是这样:</p>
<pre lang="cpp">        CBitmap* oldObject = CompatibleDC.SelectObject(&amp;bmp);

        //
        // draw minutes/seconds, using hte digits in the bitmap...
        //
        pDC-&gt;StretchBlt(200,565,87,135, &amp;CompatibleDC,(bitmap.bmWidth/12) * minute,
                0, bitmap.bmWidth/12,bitmap.bmHeight,SRCCOPY);
        pDc-&gt;SelectObject (oldObject);  //select the old object.!
        ....</pre>
<p>这样应该就可把这个问题修好啦! (没有机会测试, 但应该没有问题…).</p>
<p>再另外, 朋友告诉我他们找了一个人修了一下, 他们的方法是把那个CBitmap对象变成一个全局对象, 这样只要load一次, 所以也不会出现严重的泄漏情况(顶多一个gdi 对象, 另外效率相对每次都要load要好一些), 所以也可以算一个方法:)</p>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a><strong><em>Bookmark to:</em></strong></a>
<br />
<div class="d">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http%3A%2F%2Fblog.pangwa.com%2F2009%2F04%2F22%2Fflashing-bomb%2F&amp;title=%E9%97%AA%E5%B1%8F%E9%97%AE%E9%A2%98" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://blog.pangwa.com/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fblog.pangwa.com%2F2009%2F04%2F22%2Fflashing-bomb%2F&amp;title=%E9%97%AA%E5%B1%8F%E9%97%AE%E9%A2%98" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://blog.pangwa.com/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http%3A%2F%2Fblog.pangwa.com%2F2009%2F04%2F22%2Fflashing-bomb%2F" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://blog.pangwa.com/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http%3A%2F%2Fblog.pangwa.com%2F2009%2F04%2F22%2Fflashing-bomb%2F&amp;title=%E9%97%AA%E5%B1%8F%E9%97%AE%E9%A2%98" rel="nofollow" title="Add to&nbsp;Google Bookmarks"><img class="social_img" src="http://blog.pangwa.com/wp-content/plugins/social-bookmarks/images/google.png" title="Add to&nbsp;Google Bookmarks" alt="Add to&nbsp;Google Bookmarks" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http%3A%2F%2Fblog.pangwa.com%2F2009%2F04%2F22%2Fflashing-bomb%2F&amp;title=%E9%97%AA%E5%B1%8F%E9%97%AE%E9%A2%98" rel="nofollow" title="Add to&nbsp;reddit"><img class="social_img" src="http://blog.pangwa.com/wp-content/plugins/social-bookmarks/images/reddit.png" title="Add to&nbsp;reddit" alt="Add to&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fblog.pangwa.com%2F2009%2F04%2F22%2Fflashing-bomb%2F&amp;title=%E9%97%AA%E5%B1%8F%E9%97%AE%E9%A2%98" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://blog.pangwa.com/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://myweb2.search.yahoo.com/myresults/bookmarklet?u=http%3A%2F%2Fblog.pangwa.com%2F2009%2F04%2F22%2Fflashing-bomb%2F&amp;t=%E9%97%AA%E5%B1%8F%E9%97%AE%E9%A2%98" rel="nofollow" title="Add to&nbsp;Yahoo My Web"><img class="social_img" src="http://blog.pangwa.com/wp-content/plugins/social-bookmarks/images/yahoo.png" title="Add to&nbsp;Yahoo My Web" alt="Add to&nbsp;Yahoo My Web" /></a>
<br />
</div>
</div>
<!-- Social Bookmarks END -->
]]></content:encoded>
			<wfw:commentRss>http://blog.pangwa.com/2009/04/22/flashing-bomb/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

