当前位置: 首页 > news >正文

WinCE平台下C#引用API(GDI)一个值得警惕的内存泄漏

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

 


 
  1. [DllImport("\\windows\\coredll.dll", EntryPoint = "RoundRect")]  
  2.         private static extern int CeRoundRect(IntPtr hdc, int X1, int Y1, int X2, int Y2, int X3, int Y3);  

 这是有内存泄漏的源码:

 


 
  1. public static int RoundRect(Graphics e, Pen pen, SolidBrush brush, int X1, int Y1, int X2, int Y2, int X3, int Y3)  
  2.         {           
  3.             IntPtr hpen;  
  4.             IntPtr hbrush;  
  5.  
  6.             if(pen!=null)  
  7.             {  
  8.                 hpen = CreatePen((DashStyle.Solid == pen.DashStyle) ? 0 : 1,  
  9.                 (int)pen.Width, SetRGB(pen.Color.R, pen.Color.G, pen.Color.B));      //创建GDI画笔    
  10.             }  
  11.             else  
  12.             {  
  13.                 hpen = GetStockObject(8);      //空画笔  
  14.             }            
  15.  
  16.             if (brush!= null)  
  17.             {  
  18.                 hbrush = CreateSolidBrush(SetRGB(brush.Color.R, brush.Color.G, brush.Color.B)); //brush.Color.ToArgb());  
  19.             }  
  20.             else  
  21.             {  
  22.                 hbrush = GetStockObject(5);  
  23.             }  
  24.  
  25.             //pen.Dispose();  
  26.             //brush.Dispose();  
  27.  
  28.             IntPtr hdc = e.GetHdc();  
  29.             //---------------------     
  30.              SelectObject(hdc, hbrush);  
  31.              SelectObject(hdc, hpen);  
  32.  
  33.             int intRet=RoundRect(hdc, X1, Y1, X2,Y2, X3, Y3);  
  34.  
  35.             DeleteObject(hbrush);  
  36.             DeleteObject(hpen);  
  37.             //---------------------  
  38.             e.ReleaseHdc(hdc);  
  39.             return intRet;  
  40.         }  
  41.  

这是没有问题的源码:


 
  1. public static int RoundRect(Graphics e, Pen pen, SolidBrush brush, int X1, int Y1, int X2, int Y2, int X3, int Y3)  
  2.        {           
  3.            IntPtr hpen,old_pen;  
  4.            IntPtr hbrush, old_brush;  
  5.  
  6.            if(pen!=null)  
  7.            {  
  8.                hpen = CreatePen((DashStyle.Solid == pen.DashStyle) ? 0 : 1,  
  9.                (int)pen.Width, SetRGB(pen.Color.R, pen.Color.G, pen.Color.B));      //创建GDI画笔    
  10.            }  
  11.            else 
  12.            {  
  13.                hpen = GetStockObject(8);      //空画笔  
  14.            }            
  15.  
  16.            if (brush!= null)  
  17.            {  
  18.                hbrush = CreateSolidBrush(SetRGB(brush.Color.R, brush.Color.G, brush.Color.B)); //brush.Color.ToArgb());  
  19.            }  
  20.            else 
  21.            {  
  22.                hbrush = GetStockObject(5);  
  23.            }  
  24.  
  25.            //pen.Dispose();  
  26.            //brush.Dispose();  
  27.  
  28.            IntPtr hdc = e.GetHdc();  
  29.            //---------------------     
  30.            old_brush=SelectObject(hdc, hbrush);  
  31.            old_pen=SelectObject(hdc, hpen);  
  32.              
  33.            int intRet=RoundRect(hdc, X1, Y1, X2,Y2, X3, Y3);  
  34.  
  35.            SelectObject(hdc, old_brush);  
  36.            SelectObject(hdc, old_pen);  
  37.            DeleteObject(hbrush);  
  38.            DeleteObject(hpen);  
  39.            //---------------------  


            e.ReleaseHdc(hdc);
            return intRet;
        }

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

 

 

相关文章:

  • 我在努力学习
  • 还是到博客园安家了
  • [ IO.File ] FileSystemWatcher
  • T-SQL遗漏值NULL
  • 通过COM来获取CookieContainer,简单又好用
  • 一个实例来简单的说明接口
  • ASP.NET 2.0 Web Part编程之定制Web Part
  • .NET 指南:抽象化实现的基类
  • 浮动静态路由
  • 范伟导老师Sniffer课程资料
  • 春节期间新闻回顾:思科微软多事 熊猫烧香完事
  • Windows Mobile 6 SDK 正式发布!
  • 怎样用javascript操作ftb编辑区内容
  • 局域网防雷电***实用解决方案
  • 系统不显示桌面的原因和解决方法
  • [分享]iOS开发-关于在xcode中引用文件夹右边出现问号的解决办法
  • dva中组件的懒加载
  • echarts花样作死的坑
  • JavaScript实现分页效果
  • JS字符串转数字方法总结
  • leetcode讲解--894. All Possible Full Binary Trees
  • Node项目之评分系统(二)- 数据库设计
  • Otto开发初探——微服务依赖管理新利器
  • SpiderData 2019年2月16日 DApp数据排行榜
  • Tornado学习笔记(1)
  • 如何在GitHub上创建个人博客
  • 学习Vue.js的五个小例子
  • Java数据解析之JSON
  • 好程序员大数据教程Hadoop全分布安装(非HA)
  • ​configparser --- 配置文件解析器​
  • # 手柄编程_北通阿修罗3动手评:一款兼具功能、操控性的电竞手柄
  • #gStore-weekly | gStore最新版本1.0之三角形计数函数的使用
  • #if 1...#endif
  • #mysql 8.0 踩坑日记
  • #Z0458. 树的中心2
  • $.ajax()
  • $redis-setphp_redis Set命令,php操作Redis Set函数介绍
  • (C#)if (this == null)?你在逗我,this 怎么可能为 null!用 IL 编译和反编译看穿一切
  • (C#)获取字符编码的类
  • (Matalb回归预测)PSO-BP粒子群算法优化BP神经网络的多维回归预测
  • (附源码)计算机毕业设计ssm-Java网名推荐系统
  • (一)UDP基本编程步骤
  • (转载)(官方)UE4--图像编程----着色器开发
  • (转载)从 Java 代码到 Java 堆
  • *p++,*(p++),*++p,(*p)++区别?
  • .net framework profiles /.net framework 配置
  • @Data注解的作用
  • @property括号内属性讲解
  • [100天算法】-x 的平方根(day 61)
  • [Android Pro] listView和GridView的item设置的高度和宽度不起作用
  • [android] 切换界面的通用处理
  • [AR Foundation] 人脸检测的流程
  • [Arduino学习] ESP8266读取DHT11数字温湿度传感器数据
  • [autojs]autojs开关按钮的简单使用
  • [C#基础知识系列]专题十七:深入理解动态类型