帮助解决内存泄漏:从文件 init NSMutableArray

发布于 2024-12-02 20:37:44 字数 703 浏览 1 评论 0原文

在我的应用程序中的某些时候,我需要从文件加载列表,因此我实现此方法来加载列表:

-(void)loadList
{
    NSString *filePath = [self dataFilePath];  //This is a method return the path of file
    if([[NSFileManager defaultManager] fileExistsAtPath:filePath])
    {
        NSMutableArray *tempArray = [[NSMutableArray alloc]initWithContentsOfFile:filePath];
        self.list = [[NSMutableArray alloc]initWithArray:tempArray];
        [tempArray release];
    }
}

self.list 是一个(保留)属性。

我认为泄漏来自于我初始化 selfl.list 时的 [alloc]。我使用了

self.list = [[[NSMutableArray alloc]initWithArray:tempArray]autorelease];

但是应用程序由于 EXC_BAD_ACCESS 而崩溃。所以我在这里很困惑如何解决这个问题。

感谢您的任何建议。

In some point in my app, I need to load a list from a file, so I implement this method to load the list:

-(void)loadList
{
    NSString *filePath = [self dataFilePath];  //This is a method return the path of file
    if([[NSFileManager defaultManager] fileExistsAtPath:filePath])
    {
        NSMutableArray *tempArray = [[NSMutableArray alloc]initWithContentsOfFile:filePath];
        self.list = [[NSMutableArray alloc]initWithArray:tempArray];
        [tempArray release];
    }
}

The self.list is a (retain) property.

I think the leak is from [alloc] when I init the selfl.list. I used

self.list = [[[NSMutableArray alloc]initWithArray:tempArray]autorelease];

But the app crashes due to EXC_BAD_ACCESS. So I am confused here how to solve this.

Thanks for any suggestions.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

生生不灭 2024-12-09 20:37:44

只需分配即可,

self.list = tempArray;

由于 tempArray 已经是一个数组,因此您不必从中创建另一个数组。您可以直接分配给self.list

Just assign,

self.list = tempArray;

As tempArray is already an array, you don't have to create another array from it. You ca directly assign to self.list.

[旋木] 2024-12-09 20:37:44
  There is no need to allocate another time for array .So just assign directly

    -(void)loadList
   {
NSString *filePath = [self dataFilePath];  //This is a method return the path of file
if([[NSFileManager defaultManager] fileExistsAtPath:filePath])
{
    NSMutableArray *tempArray = [[NSMutableArray alloc]initWithContentsOfFile:filePath];
    self.list = [tempArray copy];
    [tempArray release];
}
 }
  There is no need to allocate another time for array .So just assign directly

    -(void)loadList
   {
NSString *filePath = [self dataFilePath];  //This is a method return the path of file
if([[NSFileManager defaultManager] fileExistsAtPath:filePath])
{
    NSMutableArray *tempArray = [[NSMutableArray alloc]initWithContentsOfFile:filePath];
    self.list = [tempArray copy];
    [tempArray release];
}
 }
情深如许 2024-12-09 20:37:44

不要自动释放它。 (我猜)。

Don't autorelease it. (I guess).

蓝咒 2024-12-09 20:37:44

您的清单属性是分配还是保留?
如果是保留,那么你应该将其更改

self.list = [[NSMutableArray alloc]initWithArray:tempArray];

为:

self.list = [[NSMutableArray arrayWithArray:tempArray];

is your list property assign or retain?
if it is retain, then you should change this:

self.list = [[NSMutableArray alloc]initWithArray:tempArray];

to this:

self.list = [[NSMutableArray arrayWithArray:tempArray];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文