iphone编程&x2B;内存泄漏

iphone编程&x2B;内存泄漏,iphone,memory,memory-leaks,Iphone,Memory,Memory Leaks,我得到内存泄漏theFileName=[[ResponseStringLastPathComponent]stringByDeletingPathExtension] 文件名是一个全局变量。我已经合成了它,并且 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { if ((self = [super initWithNibName:nibNameOrNil bundle:ni

我得到内存泄漏
theFileName=[[ResponseStringLastPathComponent]stringByDeletingPathExtension]

文件名是一个全局变量。我已经合成了它,并且

- (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 *jpg=@".jpg";
    NSString *addjpg=[theFileName stringByAppendingString:jpg];
    //NSLog(@"append %@",addjpg);
}
在Dealoc发布了它

-(void)dealloc
{
[thefileName release];
}
}
为文件名
创建一个新对象,该文件名已包含一个
NSString
对象。您需要
在之前释放该旧值,即

[theFileName release];
theFileName = [[responseString lastPathComponent]stringByDeletingPathExtension];
您可以考虑使用<代码>拷贝<代码>(推荐)或<代码>留存< /代码>属性为<代码>文件名,并使用代码语法>代码>请求完成:< /C> > /P> 为文件名
创建一个新对象,该文件名已包含一个
NSString
对象。您需要
在之前释放该旧值,即

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

您可以考虑使用<代码>拷贝<代码>(推荐)或<代码>留存< /COD>属性>代码>文件名,并使用代码语法>代码>请求完成:

< p>这里可能有一些帮助。

  • 您没有在
    self
    dealloc
    中调用
    super
    dealloc
    方法。比如说,

    -(无效)解除锁定
    {
    [self.theFileName发布];
    [super dealoc];
    }

  • 您没有使用合成属性时附带的getter和setter,我们也不知道您对filename使用了什么属性。如果您有一个保留属性,即类似于
    @property(copy)NSString*theFileName
    的语句,那么您应该使用setter,以免在保留计数时出错。比如说,

    • (id)initWithNibName:(NSString*)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil { if((self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) { //自定义初始化 NSString*aFileName=[[NSString alloc]init]; [自设置文件名:aFileName]; [文件名发布]; } 回归自我; }

  • 更好。

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

  • 您没有在
    self
    dealloc
    中调用
    super
    dealloc
    方法。比如说,

    -(无效)解除锁定
    {
    [self.theFileName发布];
    [super dealoc];
    }

  • 您没有使用合成属性时附带的getter和setter,我们也不知道您对filename使用了什么属性。如果您有一个保留属性,即类似于
    @property(copy)NSString*theFileName
    的语句,那么您应该使用setter,以免在保留计数时出错。比如说,

    • (id)initWithNibName:(NSString*)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil { if((self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) { //自定义初始化 NSString*aFileName=[[NSString alloc]init]; [自设置文件名:aFileName]; [文件名发布]; } 回归自我; }

  • 更好。

    有没有可能您没有调用dealloc,您是如何确定这与内存泄漏有关的?@James:当然,要明确的是,除了self的
    dealloc
    中的
    super
    之外,您自己从来没有调用过
    -dealloc
    。说到这里,您也没有在
    super
    @SK9上调用
    dealloc
    ——谢谢。我已经习惯C/C++了,周一刚开始用Objective-C进行编程。@James:请纠正我的错误,但是
    dealloc
    类似于
    C++
    中的解构器,你自己永远不会这么称呼它。相反,如果我没记错的话,在
    C++
    中,当您声明
    delete
    时会自动调用它。祝你好运并享受你的学习-我从这个网站学到了很多有没有可能你没有调用dealloc,你是如何确定这与你的内存泄漏有关的?@James:当然,要明确的是,除了self的
    dealloc
    中的
    super
    之外,你自己从来没有调用过
    -dealloc
    。说到这里,您也没有在
    super
    @SK9上调用
    dealloc
    ——谢谢。我已经习惯C/C++了,周一刚开始用Objective-C进行编程。@James:请纠正我的错误,但是
    dealloc
    类似于
    C++
    中的解构器,你自己永远不会这么称呼它。相反,如果我没记错的话,在
    C++
    中,当您声明
    delete
    时会自动调用它。祝你好运并享受你的学习-我从这个网站学到了很多@属性(非原子,保留)NSString*文件名;这就是我分配属性的方式。然后只需使用self.theFileName=[[responseString…]…]@属性(非原子,保留)NSString*文件名;这就是我分配属性的方式。然后只需使用self.theFileName=[[responseString…]…];