手机分类
  导航: 电脑时代在线 · 网站开发 · ASP.NET教程 · Asp.NET开发技巧
有问题吗?看 疑难解答 电脑术语

WinCE平台下C#引用API(GDI)里面关注内存条泄漏

 
硬件盘点
手机推荐
 
『WinCE平台下C#引用API(GDI)里面关注内存条泄漏』如果文章有大量图片,显示会较慢,请等待图片下载完成
 
点击数: 更新时间:2006-12-6 

由于C#精简框架集绘图函数不支持圆角矩形,所以引用了相关的API。

[DllImport("\\windows\\coredll.dll", EntryPoint = "RoundRect")]

private static extern int CeRoundRect(IntPtr hdc,

int X1, int Y1, int X2, int Y2, int X3, int Y3);

这是有内存泄漏的源码:

public static int RoundRect(Graphics e, Pen pen, SolidBrush brush,

int X1, int Y1, int X2, int Y2, int X3, int Y3)

{        

IntPtr hpen;

IntPtr hbrush;

if(pen!=null)

{

hpen = CreatePen((DashStyle.Solid == pen.DashStyle) ? 0 : 1,

(int)pen.Width, SetRGB(pen.Color.R, pen.Color.G, pen.Color.B)); //创建GDI画笔 

}

else

{

hpen = GetStockObject(8);      //空画笔

}         

if (brush!= null)

{

hbrush = CreateSolidBrush(SetRGB(brush.Color.R, brush.Color.G, brush.Color.B));

//brush.Color.ToArgb());

}

else

{

hbrush = GetStockObject(5);

}

//pen.Dispose();

//brush.Dispose();

IntPtr hdc = e.GetHdc();

//---------------------  

SelectObject(hdc, hbrush);

SelectObject(hdc, hpen);

int intRet=RoundRect(hdc, X1, Y1, X2,Y2, X3, Y3);

DeleteObject(hbrush);

DeleteObject(hpen);

//---------------------

e.ReleaseHdc(hdc);

return intRet;

}

这是没有问题的源码:

public static int RoundRect(Graphics e,

Pen pen, SolidBrush brush, int X1, int Y1, int X2,

int Y2, int X3, int Y3)

{        

IntPtr hpen,old_pen;

IntPtr hbrush, old_brush;

if(pen!=null)

{

hpen = CreatePen((DashStyle.Solid == pen.DashStyle) ? 0 : 1,

(int)pen.Width, SetRGB(pen.Color.R, pen.Color.G, pen.Color.B));     

//创建GDI画笔 

}

else

{

hpen = GetStockObject(8);      //空画笔

}         

if (brush!= null)

{

hbrush = CreateSolidBrush(SetRGB(brush.Color.R,

brush.Color.G, brush.Color.B)); //brush.Color.ToArgb());

}

else

{

hbrush = GetStockObject(5);

}

//pen.Dispose();

//brush.Dispose();

IntPtr hdc = e.GetHdc();

//---------------------  

old_brush=SelectObject(hdc, hbrush);

old_pen=SelectObject(hdc, hpen);



int intRet=RoundRect(hdc, X1, Y1, X2,Y2, X3, Y3);

SelectObject(hdc, old_brush);

SelectObject(hdc, old_pen);

DeleteObject(hbrush);

DeleteObject(hpen);

//---------------------

e.ReleaseHdc(hdc);

return intRet;

}

看出代码的区别来了没有?泄漏的原因其实很简单,就是没有重新选入旧的画笔画刷。同样的程序(当然PC端的API库是GDI32)在上位机Window XP平台上没有什么问题(测试大约3天以上),而在WinCE平台确非常明显,大约1~3个小时(视圆角矩形绘图的多寡而定),该程序就会内存耗尽而死。

来源链接:http://blog.csdn.net/yefanqiu/archive/2006/12/05/1430411.aspx

(68476636-8007)




如果您有什么疑问,可以至论坛提出或者解答他人的疑问   返回页面顶部

】【关闭窗口
·上一篇教程:
·下一篇教程: 没有了
·导航: 电脑时代在线 · 网站开发 · ASP.NET教程 · Asp.NET开发技巧
相关文章
 
搞笑自拍|图片故事|美女图库|体坛宝贝|明星爆料|世界奇观|风光摄影|历史回忆|大千世界
Photshop超炫图片
advertisement
关于站点 - 广告服务 - 联系我们 - 版权隐私 - 免责声明 - 合作伙伴 - 程序支持 - 网站地图 - 返回顶部  
网站文本地图
  版权所有:电脑时代在线 2005-2007 欢迎各种媒体转载我们的原创作品[转载请注明出处]
copyright © 2005-2007 www.PCvz.com online services. all rights reserved. 蜀ICP备05015578
Template designed by LaoJiang. Optimized for 1024x768 to Firefox,Opera and MS-IE. Site powered by EQL.
红盾
热爱电脑,热爱生活
拥有电脑,拥有生命
让我们享受拥有电脑的时光