图形图像 多媒体类 机械电子 办公系列 程序设计 网站开发 数据库教程 路由技术 网络原理 网络应用 考试认证
手机分类
  网站导航: 电脑时代在线 · 网站开发 · 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)



】【关闭窗口
  上一页:
  下一页:后面的文章暂时没有更新了,请多关注QQgb.com
Asp.NET开发技巧:相关文章
Asp.NET开发技巧点击榜
推荐教程WinCE平台下C#引用API(GDI)里
推荐教程ASP.NET 2.0里面直接将Access数
推荐教程ASP.NET模拟其余用户进行关机
推荐教程在VB.NET里面怎么完成多窗体同步
推荐教程.NET 地发展与变迁(1)
推荐教程.NET(C#)连接各类数据库-集锦
推荐教程配置EditPlus与NAnt构建轻型地.
推荐教程浅谈.NET里地MTS
推荐教程.NET里面异常处理最佳实践(1)
推荐教程ASP.NET 2.0里面层次数据地处理
搞笑自拍|图片故事|美女图库|体坛宝贝|明星爆料|世界奇观|风光摄影|历史回忆|大千世界
Asp.NET开发技巧推荐图片
advertisement
关于站点 - 广告服务 - 联系我们 - 版权隐私 - 免责声明 - 合作伙伴 - 程序支持 - 网站地图 - 返回顶部  
网站文本地图
  版权所有:电脑时代在线 2005-2009 欢迎各种媒体转载我们的原创作品[转载请注明出处]
copyright © 2005-2007 www.PCvz.com online services. all rights reserved. 蜀ICP备05015578
Template designed by PCX. Optimized for 1024x768 to Firefox,Opera and MS-IE. Site powered by EQL.
红盾
热爱电脑,热爱生活
拥有电脑,拥有生命
让我们享受拥有电脑的时光