是静态类成员寄托? [英] Are static class members pinned?
问题描述
我有一个C#类,有一个静态的ImageList对象。此图片名单将在我的应用程序的多个形式多样的ListView头(通过SendMessage函数... HDM_SETIMAGELIST)共享。
I have a C# class, having a static ImageList object. This image list will be shared with various ListView headers (via SendMessage... HDM_SETIMAGELIST) on several forms in my application.
虽然我明白,静态对象不符合垃圾收集,这是我不清楚,如果他们还没有资格获得搬迁(压缩)被垃圾收集器。难道我还需要这脚对象,因为它是与非托管代码共享,也就是说,使用GCHandle.Alloc?
Though I understand that static objects are not eligible for garbage collection, it is not clear to me if they are also ineligible for relocation (compaction) by the garbage collector. Do I also need to pin this object since it is shared with unmanaged code, say, using GCHandle.Alloc?
环境是VS 2008,Compact Framework的3.5。
Environment is VS 2008, Compact Framework 3.5.
推荐答案
该实例本身也不是一成不变的。该参考。如果你空引用的实例就符合了GC。在内部,所有静态实例是通过一个固定手柄静态数组引用。即实例是隐式运行时固定。
The instance itself is not static. The reference is. If you null the reference the instance becomes eligible for GC. Internally, all static instances are references through a pinned handle to an array of statics. I.e. the instance is implicitly pinned by the runtime.
如果你看一下声明为静态成员实例的GCroot,你会看到这样的事情:
If you look at the GCroot of an instance declared as a static member, you'll see something like this:
HandleTable:
008113ec (pinned handle)
-> 032434c8 System.Object[]
-> 022427b0 System.Collections.Generic.List`1[[System.String, mscorlib]]
如果您空静态参考固定阵列中的相应项置空为好。
If you null the static reference the corresponding entry in the pinned array is nulled as well.
现在,这些都是明显的实施细则,使他们有可能改变。
Now, these are obviously implementation details so they could potentially change.
这篇关于是静态类成员寄托?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!