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

news/2024/7/5 14:29:01

由于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://www.niftyadmin.cn/n/3655741.html

相关文章

Object.getOwnPropertyDescriptors(data)获取描述符

Object.getOwnPropertyDescriptors(data) 获取objcet 对象所有数据的描述符 Object.getOwnPropertyDescriptor(data,“key”) 获取单个数据的描述符 该方法允许对一个属性的描述进行检索。在 Javascript 中, 属性 由一个字符串类型的“名字”&#xff0…

WinCE 4.2(.net精简框架集)下的图形双缓存处理

在上位机Windows操作平台( .net 2.0框架集)下实现图形双缓存有很多办法,每种办法在上位机都有很好的效果,但是很不幸,在WinCE4.2操作系统(.net 2.0精简框架集)下这些方法要么不支持,…

for await of

for of方法能够遍历具有Symbol.iterator接口的同步迭代器数据,但是不能遍历异步迭代器。ES9新增的for await of可以用来遍历具有Symbol.asyncIterator方法的数据结构,也就是异步迭代器,且会等待前一个成员的状态改变后才会遍历到下一个成员&a…

嵌入式EasyHMI V0.1版终于推出,C#真是软件开发的利器

这段时间就和闭关一样,连续几周一直在开发嵌入式EasyHMI,从架构到具体的代码,有上位机的IDE开发环境到WinCE上的运行时环境,有TCP/IP、COM通信,有多线程,多缓存图形处理,XML序列化,有…

MOXA的智能通信产品也大力支持WinCE.net了

10月26日参加了moxa公司举办的一个产品交流会, moxa的产品线又得到了进一步的拓宽,其中我对新出的UC系列的智能通信服务器比较感兴趣。以前moxa的智能产品的操作系统一般是uclinux/Linux,目前大部分智能产品都开始支持WinCE.net(5.0)了&#…

在ES9新增的Object的RestSpread方法有何用武之地

把两个对象合到一起 const input { a:1, b:2 } const output { ...input, // Spread 用拷贝的方法 拷贝到output c:3 } input.a 4; // 这里只改了 input 并没有改 output console.log(input) // a:4 b:2 console.log(output) // a:1 b:2 c:3 把一个对象拆…

node.js 写博客系统1

1. npm init -y 初始化环境 2. npm install lodash --save 常用的工具类 3. http 请求概述 DNS解析,建立tcp链接,发送http请求 server接收到HTTP请求,处理,并返回 客户端接收到返回数据,处理数据(如渲…

关于VS2005与EVC4.2的项目开发过程中的问题点滴

这段时间一直致力于嵌入式IOServer与嵌入式HMI的开发,这中间及牵扯C#与EVC通信的问题,也牵扯EVC本身开发遇到的一些问题。1、EVC与C#数据传递我是用EVC做DLL(MFC 扩展DLL),C#直接调用…