认真的程序员最可爱

记录一段小经历,当我们嗅觉到一段有问题的代码时,仔细研究下会有不错的发现。 

经验总结:当我们跟IntPtr打交道时一定要小心谨慎,谨防GDI泄漏

 

Yesterday, I noticed that some image resources in Annotation project are not defined as static, the client directly access by “Properties.Resources…”.

As far as I know is, in original we have a ‘rule’ that the image resource should defined as static in order to defense GDI leak. (I’m not sure about this, because I could not find out the related mail now.)

 

So, I test the annotation ribbon, and found there does exist GDI leak problem. If we change the callout shape type or toggle “Keep On Top” check box continuously, the GDI increased shapely, when it reach to 9999, the Application crashed. Please refer to 【...】.

Jerry has helped me located the key problem, that in AnimatedBusyCursor.cs class, line 66, it create a Cursor by IntPtr, but never destroy it. Thanks, Jerry, it’s a amazing found.

 

I have downloaded his code and tested, it works well. All the increased GDI object can be decrease now. So I’m not sure if we still need defined the image resource as static. In 【...】, I does defined the image resource as static. Please help me to review it. If you don’t mind, I want to check in those code. Any problem, please let me know.

 

(It is strange to me that why it is ok now we directly use the image resource. I guess it may related to the some Contextual Ribbon changes. In early time, the contextual ribbon for annotation can have several copies at the same time, it will created a copy when show annotation’s contextual ribbon, and never released.  So if we don’t defined the image as static, it may reference more than once by the ribbon control, so the GDI increased. Since, currently this problem has been solved, there is only one copy contextual ribbon, so maybe directly access the image is ok. I’m not sure, just guessingJ)

posted @   Anders06  阅读(288)  评论(0编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
点击右上角即可分享
微信分享提示