使用Swift的内存管理问题

时间:2016-03-30 08:26:41

标签: ios swift memory-management deinit

我们遇到了一些内存分配问题。 当我运行我的应用程序并进入某个页面时,每次访问页面时内存计数都会增加30mb。

我已将IBOutlets添加为Weak并将变量声明为? (即Optionals),虽然内存没有释放对象,但是当我卸载页面时,deinit()没有被调用。

我的代码
-----------

声明

//[Mapped Outlet With Front End]  
@IBOutlet weak var testInd: UIActivityIndicatorView!  
@IBOutlet weak var uiPendingView: UIView!  
@IBOutlet weak var uiDraftView: UIView!  
@IBOutlet weak var propertyListingTableView: UITableView!  
@IBOutlet weak var uiCompletedView: UIView!  
@IBOutlet weak var propertiesMap: MKMapView!  
@IBOutlet weak var btnSynch: UIButton!  
@IBOutlet weak var lblPendingStatus: UILabel!  
@IBOutlet weak var lblDraftStatus: UILabel!  
@IBOutlet weak var lblCompleteStatus: UILabel!
@IBOutlet weak var networkLabel: UILabel!
@IBOutlet weak var tableEmtyLabel: UILabel!  
@IBOutlet weak var lblPropertyLabel: UILabel!  
@IBOutlet weak var lblPendingLabel: UILabel!  
@IBOutlet weak var lblDraftLabel: UILabel!  
@IBOutlet weak var lblCompletedLabel: UILabel!  
@IBOutlet weak var segmentControllerSynch: UISegmentedControl!  
@IBOutlet weak var googleMapView: GMSMapView!  
@IBOutlet weak var propertyLabelView: UIView!  

//[Local variable declaration]  
var annotation: CustomPointAnnotation?  
var annotationArray : [CustomPointAnnotation]?
var properties:[Property] = [Property]()
var tempProperties:[Property] = [Property]()
var pushProperties:[Property] = [Property]()
var getProperties:[Property] = [Property]()
var draftProperties:[Property] = [Property]()
var failedProperties:[Property] = [Property]()
var property: Property?
var company: Company?
var companies: [Company]?
var allProperties: [Property]?
var xmlElementData : NSData!
var filePath:NSString?
var xmlParseString: NSString?
var xmlNSData: NSData?
var pendingFilterTick = false
var draftFilterTick = false
var compeletdFilterTick = false
var fromPopover = false
var errorStr: String?
var tokenStr: String?

var resultDict: NSDictionary?
var sortingImg   : UIImage = UIImage(named: "Sorting")!
var menuImg : UIImage = UIImage(named: "Slider")!
var leftLogo : UIImage = UIImage(named: "logo")!
var sortingBtn : UIBarButtonItem?
var menuBtn : UIBarButtonItem?
var leftBtn : UIBarButtonItem?
var emptyView: UIView!
var actInd: UIActivityIndicatorView?
var container: UIView?
var lblProcess: UILabel?
var loadingView: UIView?
let nsFileManager = NSFileManager.defaultManager()

//[Local class declaration]
var constants = Constants()
var parser = XMLParser()
var utility = Utility()
var fileMgr = FileManagement()
var xmlParser = XMLParser()
var netUtil = NetworkUtil()
var dict:Dictionary<String,String> = Dictionary<String,String>()
let appDelegate = UIApplication.sharedApplication().delegate as? AppDelegate
var unitCount = 0
var dateUtil = DateUtils()
var synchCompanies:[Company]?
var controllerUtil = ControllerUtil()
var errorHandling = ErrorHandling()

//[Variable declaration for the Google map]
var locationManager = CLLocationManager()
var didFindMyLocation = false

override func viewDidDisappear(animated: Bool) {
    super.viewDidDisappear(animated)

    print("viewDidDisappear")

    if let superView = self.view.superview
    {
        superView.removeFromSuperview()
    }

    fileMgr.deallocateReferences(xmlNSData!)
   // fileMgr: FileManagement = nil

    tempProperties = []
    pushProperties = []
    getProperties = []
    draftProperties  = []
    failedProperties = []


//        testInd = nil  
//        uiPendingView = nil  
//        uiDraftView = nil  
//        propertyListingTableView = nil  
//        uiCompletedView = nil  
//        propertiesMap = nil  
//        btnSynch = nil   
//        lblPendingStatus = nil  
//        lblDraftStatus = nil  
//        lblCompleteStatus = nil   
//        networkLabel = nil   
//        tableEmtyLabel = nil  
//        lblPropertyLabel = nil  
//        lblPendingLabel = nil  
//        lblDraftLabel = nil  
//        lblCompletedLabel = nil  
//        segmentControllerSynch = nil  
//        googleMapView = nil  
//        propertyLabelView = nil  


}
deinit{
    print("DEINIT")
   googleMapView.removeObserver(self, forKeyPath: "myLocation", context: nil)
}

1 个答案:

答案 0 :(得分:0)

如果未调用视图控制器的deinit,则有人会对其进行强烈引用。找到罪魁祸首。

顺便说一句。使用超级视图可能不是一个好主意。这可能会造成各种麻烦。