Win32.DestroyIcon与Icon.Dispose [英] Win32.DestroyIcon vs. Icon.Dispose

查看:223
本文介绍了Win32.DestroyIcon与Icon.Dispose的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这行代码:

  System.Drawing.Icon icon = System.Drawing.Icon.FromHandle(shinfo .hIcon); 

几行后,使用图标后,我有行:

  Win32.DestroyIcon(shinfo.hIcon); 

然而,在我的代码上运行静态分析时,它说图标中存在Resource_Leak的潜在可能性。我想知道如果我调用dispose方法,它会起什么作用:

  icon.Dispose(); 

而不是现在正在使用的Win32.DestroyIcon()。他们之间有什么区别吗?我只是维护这段代码,所以我不确定是否有原始开发人员使用Win32.DestroyIcon的任何特殊插入。

解决方案

静态分析正在触发,因为您没有处理IDisposable资源。

我会建议坚持使用托管版本,并使用 icon.Dispose()。这将(内部)负责为你调用 DestroyIcon ,但始终坚持使用纯粹的托管API。



Win32.DestroyIcon 确实更适用于您正在接收的图标,作为 IntPtr ,不适用于完全由框架管理的 Icon >实例。

I have this line of code:

System.Drawing.Icon icon = System.Drawing.Icon.FromHandle(shinfo.hIcon);

A few lines later, after icon is used I have the line:

Win32.DestroyIcon(shinfo.hIcon);

However when running a static analysis on my code it says there is a potential for Resource_Leak from icon. I am wondering will it make any difference if I call the dispose method:

icon.Dispose();

rather than the Win32.DestroyIcon() that is being used right now. Is there any difference between them? I am just maintaining this code so I am not sure if there was any special intnetion by the original developer to use Win32.DestroyIcon.

解决方案

The static analysis is triggering because you aren't disposing the "IDisposable resource".

I would recommend sticking to the managed version throughout, and using icon.Dispose(). This will (internally) take care of calling DestroyIcon for you, but stick to the pure managed API throughout.

Win32.DestroyIcon is really intended more for use with icons you're receiving as an IntPtr, not for use with an Icon instance that is managed by the framework entirely.

这篇关于Win32.DestroyIcon与Icon.Dispose的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆