Leaks in Swift 3 / iOS 10

前端 未结 1 473
野性不改
野性不改 2020-12-19 07:43

When I\'m running instruments and check for leaks, it\'s showing leaks mainly consisting of:

_ContiguousArrayStorage
_NativeDictionaryStorageOw         


        
相关标签:
1条回答
  • 2020-12-19 08:38

    I had the same problem and spent a lot of time digging. I found that if you create a Swift object from Objective-C code and the Swift object has a native Swift dictionary property, you will see this leak. It won't happen if all the code is Swift, and more usefully, it won't leak if you change the native Swift dictionary to an NSDictionary. This also applies to Swift Set's and NSSet's. I also saw that the leak happens on iOS 10 and not on iOS 9.

    // LeakySwiftObject.swift
    class LeakySwiftObject: NSObject {
        let dict = [String: String]() // <- Any native Swift dictionary will reproduce the leak
    }
    
    // ObjectiveCObject.h
    @class LeakySwiftObject;
    
    @interface ObjectiveCObject : NSObject
    @property (strong) LeakySwiftObject *leaky;
    @end
    
    // ObjectiveCObject.m
    @implementation ObjectiveCObject
    
    - (instancetype)init
    {
        self = [super init];
        if (self) {
            self.leaky = [LeakySwiftObject new];
        }
        return self;
    }
    
    @end
    
    // ViewController.swift
    class ViewController: UIViewController {
        let testObj = ObjectiveCObject()
    }
    

    The Leaks Instrument reports a leak:
    _NativeDictionaryStorageImpl<String,String>
    _NativeDictionaryStorageOwner<String,String>

    0 讨论(0)
提交回复
热议问题