iphone programming + memory leak
I am getting memory leak theFileName = [[responseString lastPathComponent]stringByDeletingPathExtension];
theFileName is a global variable. I have synthesized it and
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]))
开发者_如何学Go {
// Custom initialization
theFileName = [[NSString alloc] init];
}
return self;
}
- (void)requestFinished:(ASIHTTPRequest *)request{
//internally calls this function
// Use when fetching text data
NSString *responseString = [request responseString];
//NSLog(@"the responsestring for download is:%@",responseString);
theFileName = [[responseString lastPathComponent]stringByDeletingPathExtension];
//NSLog(@"the theFileName for download is:%@",theFileName);
//adds extension .jpg to file name
NSString *jpg=@".jpg";
NSString *addjpg=[theFileName stringByAppendingString:jpg];
//NSLog(@"append %@",addjpg);
}
Released it in dealloc.
-(void)dealloc
{
[thefileName release];
}
}
theFileName = [[responseString lastPathComponent]stringByDeletingPathExtension];
creates a new object for theFileName
, which already holds an NSString
object. You need to release
that old value before, i.e.
[theFileName release];
theFileName = [[responseString lastPathComponent]stringByDeletingPathExtension];
You might consider using a copy
(recommended) or retain
property for theFilename
, and use the dot-syntax in requestFinished:
.
Here are a few things that might help.
You're not calling
super
'sdealloc
method withinself
'sdealloc
. For example,- (void) dealloc { [self.theFileName release]; [super dealloc]; }
You're not using the getters and setters that come with synthesizing a property, and we don't know what property you've used with theFileName. If you've got a retaining property, i.e. a statement like
@property (copy) NSString * theFileName
then you should use the setter so that you don't trip up on retain counts. For example,- (id) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) { // Custom initialization NSString * aFileName = [[NSString alloc] init]; [self setTheFileName:aFileName]; [aFileName release]; } return self; }
is better.
精彩评论