How am I leaking NSString in this case?
I have a tableview that has a string on the last row.
@interface MyTableViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
{
NSString *loadingMessage;
}
@property (retain) NSString *loadingMessage;
@synthesize loadingMessage;
- (UITableViewCell *)tableView:(UITableView *)pTableView cellForRowAtIndexPath:(NSIndexP开发者_如何学运维ath *)indexPath {
//some stuff...
cell.textLabel.text = loadingMessage;
}
Then when some event happens, I will be changing the loading message:
-(void)requestFailed:(NSError*)error {
self.loadingMessage = [NSString stringWithFormat:@"Failed with error: %@", error];
}
According to instruments I am leaking the loadingMessage string in here... But I don't see why. I thought the count for stringWithFormat is +0, and the setter is +1. I release the string when I dealloc as well. What am i doing wrong?
The code you posted is correct. Indeed, stringWithFormat
returns an autoreleased object, so you can assign it directly to a retained property.
So, either you are doing some other assignment in your code, or, more probably, you are not releasing loadingMessage
in your dealloc
.
Just an hypothesis.
Sergio is right , theres nothing wrong with the code but I have found that string properties need to be copy rather than retain.
Try
@property (copy,readwrite) NSString *loadingMessage;
Might stop the leak
精彩评论