Jim Blanc
Jim Blanc

Reputation: 93

Why does this code produce leaks?

A total Noob here positing for the first time on this forum.

I am writing my very first iPhone app. The code in question is to populate my UITableViewCell in the cellForRowAtIndexPath method.

When I run my app with Leaks Instrument turned on, the leaking part points to this part of my code. I have read about memory management on various books and postings on this site and per my knowledge the code below seems to conform to the required alloc/release coding guidlines. However, the Leaks tool still points to this particular section. I simply don't understand why.

Your help would be appreciated.

ModelClass * myModelData = [myTempDataArray objectAtIndex:[indexPath row]];

NSString * myURL = [[NSString alloc] initWithFormat:@"%@/images/%@", SERVER_URL, [myModelData publisherLogoFileName]];
NSURL * imageURL = [[NSURL alloc] initWithString:myURL];
[myURL release];
[[cell productImageM] setUrl:imageURL];
[imageURL release];
[[self objMan] manage:[cell productImageM]];

return cell;

Upvotes: 0

Views: 88

Answers (2)

Enzo Tran
Enzo Tran

Reputation: 5850

You can try to use the Build & Analyze option from the Build menu. In general it should indicate the exact line of the leak.

Upvotes: 0

Tommy
Tommy

Reputation: 100632

The Leaks tool doesn't tell you where the object should have been released, it tells you where it was allocated.

That bit of code looks fine; you own myURL long enough to use it to create imageURL and then release it. You give imageURL to the cell's productImageM and then release it. You then do some manipulation on objects you don't own.

At a guess, the type of object returned by [cell productImageM] retains its Url property and subsequently leaks it.

Upvotes: 1

Related Questions