How do I leak memory?

I have a table view that, when loaded, creates a person object

Person.h

#import <UIKit/UIKit.h>
#import "TwitterHelper.h"

@interface Person : NSObject {
    NSDictionary *userInfo;
    NSURL *image;
    NSString *userName;
    NSString *displayName;
    NSArray *updates;
}
/*
@property (retain) NSString *userName;
@property (retain) NSString *displayName;
@property (retain) NSDictionary *userInfo;
 */
@property (nonatomic, copy) NSURL *image;
@property (retain) NSArray *updates;

- (id)initWithUserName:userName;

@end

Person.m

#import "Person.h"


@implementation Person

/*
@synthesize userName;
@synthesize displayName;
@synthesize userInfo;
 */
@synthesize image;
@synthesize updates;

- (id)initWithUserName:(NSString *)user{

    userName = user;
    userInfo = [TwitterHelper fetchInfoForUsername:user];
    displayName = [userInfo valueForKey:@"name"];
    image = [NSURL URLWithString:[userInfo valueForKey:@"profile_image_url"]];
    updates = [TwitterHelper fetchTimelineForUsername:userName];

    return self;
}

- (void)dealloc
{
    /*
    [userName release];
    [displayName release];
    [updates release];
     [userInfo release];
     [image release];
     */
    [super dealloc];
}

@end

Inside my UITableView cellAtRowForIndexPath method, I create each object and assign an image property like this ...

Person *person = [[Person alloc] initWithUserName:userName];

NSData *data = [[NSData alloc] initWithContentsOfURL:person.image];
[data release];

When I run this in Tools, it highlights the line NSData * data ... which says it is a leak.

Why is it leaking there?

+3
source share
3 answers

First, you need to understand the difference between instance variables and properties and getter / seters.

  • (ivars) - , . ivar (, "userName" ).
  • , / .
  • getters/setters ivar .

, getter/setter, (, [self userName]), () self.userName. , . (.. ) @property , :

@property (copy) NSString* userName;

:

- (NSString*) userName;
- (void) setUserName: (NSString*) theUserName;

@synthesize ( getter/setter), (.. userName setUserName). , @dynamic, , , , .

. 9 , , . ? .

, , / init dealloc.

, :

- (id)initWithUserName:(NSString *)user{
    userName = [user copy];
    userInfo = [[TwitterHelper fetchInfoForUsername:user] retain];
    displayName = [[userInfo valueForKey:@"name"] copy];
    image = [[NSURL URLWithString:[userInfo valueForKey:@"profile_image_url"]] copy];
    updates = [[TwitterHelper fetchTimelineForUsername:userName] retain];
    return self;
}

, , ivar . , NSString NSMutableStrings NSStrings, , , . NSArray/NSDictionary, , TwitterHelper .

dealloc ivars:

- (void)dealloc
{
    [userName release];
    [displayName release];
    [updates release];
    [userInfo release];
    [image release];
    [super dealloc];
}

self.userName , ivars .

, displayName ( ), getter, userInfo. displayName ivar, :

@property ( ) NSString * displayName;

@synthesize displayName getter:

- (NSString*) displayName
{
    return [userInfo valueForKey:@"name"];
}

dealloc.

, / displayName - , , / , .

+5

, :

self.image = [NSURL URLWithString:[userInfo valueForKey:@"profile_image_url"]];

init,

image = [NSURL URLWithString:[userInfo valueForKey:@"profile_image_url"]];

self copy retain ( ).

, .

(, , !)

, , retain copy , :

image = [[NSURL URLWithString:[userInfo valueForKey:@"profile_image_url"]] retain];
+1

You challenge alloca person, but do not let him go. You have missed your object person. (in the configuration of your cell)

0
source

Source: https://habr.com/ru/post/1714994/


All Articles