简体   繁体   English

解锁功能目标

[英]Unhook Func Target

I have a Dictionary of byte[], Func as below: 我有一个byte []字典,Func如下:

 public class ImageSizeReader : IDisposable
 {

    private Dictionary<byte[], Func<BinaryReader, Size>> imageFormatDecoders;

    public ImageSizeReader()
    {
        imageFormatDecoders = new Dictionary<byte[], Func<BinaryReader, Size>>()
        {
            { new byte[] { 0x42, 0x4D }, DecodeBitmap },
            { new byte[] { 0x47, 0x49, 0x46, 0x38, 0x37, 0x61 }, DecodeGif },
            { new byte[] { 0x47, 0x49, 0x46, 0x38, 0x39, 0x61 }, DecodeGif },
            { new byte[] { 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A }, DecodePng },
            { new byte[] { 0xff, 0xd8 }, DecodeJfif }};
    }

    private Size DecodeBitmap(BinaryReader binaryReader)
    {
    ....
    }

    private Size DecodeGif(BinaryReader binaryReader)
    {
        ...
    }

    private Size DecodePng(BinaryReader binaryReader)
    {
      ...
    }

    private Size DecodeJfif(BinaryReader binaryReader)
    {
       ...
    }

}

After using Memory Profiler I notice that this Dictionary is preventing garbage collection on it's parent class: 使用Memory Profiler之后,我注意到此Dictionary阻止了对其父类的垃圾回收:

在此处输入图片说明

I am assuming it is because the Func delegate is still wired up. 我认为这是因为Func委托仍处于连接状态。 How would I 'unhook' this delegate list correctly? 我如何正确地“摘机”该代表名单?

I came back to this and eventually found that the code in the image above was compiler generated from a linq statement: 我回到这一点,最终发现上图中的代码是从linq语句生成的编译器:

int maxMagicBytesLength = imageFormatDecoders.Keys.OrderByDescending(x => x.Length).First().Length;

I removed this method and did the same in a foreach loop which resolved the issue. 我删除了此方法,并在foreach循环中执行了相同的操作,从而解决了该问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM