0

I am using swift. Have some customised UIControls and UIViews. They have been dragged into storyboard made IBOutlet with weak property.

I ran the App in Allocations Instrument and presents the viewcontroller using all these views and controls. Dismisses the controller but the objects that have been allocated are persistent in memory. On each attempt of presenting the controller, the memory grows accordingly and none of them seems to be released.

I am adding a related control here

import UIKit

@IBDesignable class CNButtonBackground: UIView {

private func initialize()
{
    //color is set through context fill in drawRect
    self.backgroundColor = UIColor.lightTextColor()
}

override init(frame: CGRect) {
    super.init(frame: frame)

    //initializing custom components
    self.initialize()
}

required init(coder: NSCoder) {
    super.init(coder: coder)

    //initializing custom components
    self.initialize()
}

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
override func drawRect(rect: CGRect)
{
    super.drawRect(rect)

    // Drawing code
    self.layer.cornerRadius = 5.0
    self.clipsToBounds = Yes

    // Fill color
    let context = UIGraphicsGetCurrentContext()
    var drawRect: CGRect = CGRectMake(rect.origin.x, rect.origin.y,rect.size.width, rect.size.height);

    CGContextSetRGBFillColor(context, 35.0/255.0, 24.0/255.0, 21.0/255.0, 0.8);
    CGContextFillRect(context, drawRect);
}
}

What is that I am missing which is causing this issue? Please help me out... Anything I could try out is also fine. Give me directions, I am willing to try all.

aksh1t
  • 5,410
  • 1
  • 37
  • 55
wolverine
  • 189
  • 5
  • 20
  • Show the code for any of the UIControl definition. – Mohd Prophet Mar 16 '15 at 04:52
  • How are you dismissing the controller? – rdelmar Mar 16 '15 at 04:54
  • Presented using self.performSegueWithIdentifier("toTheView", sender: self) Dismissed using self.dismissViewControllerAnimated – wolverine Mar 16 '15 at 04:58
  • Implement deinit in the presented view controller, and see if it's called when you dismiss. – rdelmar Mar 16 '15 at 05:00
  • Already did and its called. But too late like maybe when I am presenting some other 3rd or 4th controller the 1st one's deinit is called – wolverine Mar 16 '15 at 05:02
  • Are you using blocks anywhere in your code? Blocks will retain obj-c values which are captured when the block is created. Check out [this](https://blackpixel.com/writing/2014/03/capturing-myself.html) blog on breaking the retain cycle. – aksh1t Mar 16 '15 at 08:50
  • 1
    Also, if you are using custom delegates in your ViewControllers, make sure that they are weak. Strong delegates create retain cycles too. Refer [this](http://stackoverflow.com/questions/8449040/why-use-weak-pointer-for-delegation) – aksh1t Mar 16 '15 at 08:55
  • Thanks a lot aksh1t. That was the issue. Damn me, I dint check in all the pages. two or three of them were still declared without weak, which made them strong. – wolverine Mar 16 '15 at 17:40

0 Answers0