2011-06-28 58 views
0

我越来越内存泄露theFileName = [[responseString lastPathComponent]stringByDeletingPathExtension];iPhone编程+内存泄露

theFileName是一个全局变量。我已经合成它并且

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) 
    { 
     // 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 *[email protected]".jpg"; 
    NSString *addjpg=[theFileName stringByAppendingString:jpg]; 
    //NSLog(@"append %@",addjpg); 
} 

在dealloc中发布它。

-(void)dealloc 
{ 
[thefileName release]; 
} 
} 
+2

你可以发布你的@属性。 – tjg184

+0

有没有可能你没有调用dealloc,你是如何确定这与你的内存泄漏有关的? –

+0

@James:当然,要清楚的是,除了自己的'dealloc'中的'super'之外,您不会自己调用'-dealloc'。说到这一点,你也不会在'super'上调用'dealloc'。 – SK9

回答

1

以下是一些可能有所帮助的事情。

  1. 你不是叫super的内selfdealloc方法的dealloc。例如,

    - (void) dealloc { [self.theFileName release]; [super dealloc]; }

  2. 您没有使用附带合成的属性的getter和setter方法,我们不知道你有theFileName使用什么属性。如果你有一个保留属性,即@property (copy) NSString * theFileName这样的陈述,那么你应该使用setter,这样你就不会留下保留计数。例如,

    • (ID)initWithNibName:(的NSString *)nibNameOrNil束:(一个NSBundle *)nibBundleOrNil { 如果((个体= [超级initWithNibName:nibNameOrNil束:nibBundleOrNil])) { //自定义初始化 NSString * aFileName = [[NSString alloc] init]; [self setTheFileName:aFileName]; [aFileName release]; } return self; }

更好。

3
theFileName = [[responseString lastPathComponent]stringByDeletingPathExtension]; 

创建theFileName一个新的对象,它已经拥有了NSString对象。您需要到release旧的价值,即

[theFileName release]; 
theFileName = [[responseString lastPathComponent]stringByDeletingPathExtension]; 

你可以考虑使用一个copy(推荐)或retain财产theFilename,并在requestFinished:使用点语法。

+0

@property(nonatomic,retain)NSString * theFileName;这是我如何分配属性。 – xcodelearner

+0

然后,只需使用self.theFileName = [[responseString ...] ...]; – Eiko