Fixing memory leak issue.

Discussion in 'iOS Programming' started by mycompuser, Jul 25, 2013.

  1. mycompuser macrumors member

    Joined:
    May 8, 2012
    #1
    Hi All,

    I am developing an iOS app which is ARC enabled.

    When I ran the Instruments tool (Leaks), found that the like containing "imageNamed" method as causing memory leak.

    Here is the code snippet.
    Code:
    in .h file
    @property (nonatomic) IBOutlet UIImageView *myImg;
    in .m
    self.myImg.image = [UIImage imageNamed:@"someImage.png"];//causing memory leak
    Similarly this code was also indicating an memory leak.

    Code:
    .h
    @property (nonatomic) UIImage *iPanel;
    
    .m
    self.iPanel = [[UIImage imageNamed:@"someImage.png"] resizableImageWithCapInsets:edgeInset];
    
    I read from few links that imageNamed method can cause memory leak issues.

    But for some reasons (nearing a major release) I dont want to change imageNamed with imageWithContentsOfFile.

    Found another link which says that if you use "strong" (retain's equivalent) will fix this issue.

    Need expert's opening on this.

    Will changing the property declaration from (nonatomic) to (nonatomic, strong) fix the memory leak issue?
     
  2. dantastic macrumors 6502

    dantastic

    Joined:
    Jan 21, 2011
    #2
    You should always declare how the property is meant to behave in terms of memory, it matters!

    A quick and dirty is to declare them as strong. Your IBOutlet is likely to be assign though.
     
  3. KoolStar macrumors demi-god

    KoolStar

    Joined:
    Oct 16, 2006
    Location:
    Kentucky
    #3
    Or in the case of ios 6 and up it would be a weak property.
     
  4. mycompuser thread starter macrumors member

    Joined:
    May 8, 2012
    #4
    Guys, Thanks for the reply.

    You mean that for IBOutlet, the property need not be changed and let it remain as weak as it is an IBOutlet?

    (And for the former case, I can define the property to be strong to avoid the memory leak issue?)
     

Share This Page