认真的程序员最可爱

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

经验总结:当我们跟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 @ 2010-08-20 18:07  Anders06  阅读(160)  评论(0编辑  收藏