0
votes

here I have a tableView in my viewController, and it's like this:

//.h
...
UITableView *aTable;
...
@property (nonatomic, retain) UITableView *aTable;
//.m
@synthesize aTable;
...
self.aTable = nil; // in viewDidUnload
[aTable release];  // in dealloc
...

and now, in the viewDidLoad method I have to initialize this talbe, so I did this

// viewDidLoad
...
self.aTable = [[UITableView alloc] initWithFrame:xxxxx];
...

but when I analyze this, it reminds me that in the viewDidLoad,

there maybe a potential leak of memory of this "aTable",

but I guess I am about to release it in the dealloc, so why is there still a leak of memory?

it is about the "retain" property?

do I have to add an autorelease to the code where alloc and initiate this tableView?

Thanks a lot!

1

1 Answers

1
votes

Yes, it should be (in a non arc environment):

self.aTable = [[[UITableView alloc] initWithFrame:xxxxx] autorelease];

This give you an autoreleased object (retain count: 0)

[[[UITableView alloc] initWithFrame:xxxxx] autorelease];

This retains the table(retain count: 1) in order to persist til viewDidUnload or til Dealloc

self.aTable = ...

If you dont autorelease the table you're getting a retain count: 2 (One of the alloc-init and the other from the retain property), then on the ViewDidUnload with this line self.aTable = nil; you are decreasing the retaing count to 1, but then assigning nil to your ivar, so you're lossing the reference to the alive table hence leaking the tableView, then in dealloc you will make basically this [nil release] which has no effect.