iPhone - Objective-C - initWithArray 内存泄漏

发布于 2024-10-17 10:31:37 字数 1750 浏览 3 评论 0原文

我正在使用下面的代码来设置我的两个 NSArray ivars:

问题是,我在以下几行中不断出现内存泄漏:

followingFriendsArray = [[NSArray alloc] initWithArray:friend.Following];
followerFriendsArray = [[NSArray alloc] initWithArray:friend.Followers];

这不是从现有 设置 ivars 的正确方法吗? NSArray 项目?任何帮助将不胜感激。我还尝试过自动释放上述两行,但是当我实际用另一种方法访问它们时,我收到一个错误,表明它们已经被释放。

我在下面包含了我的接口和实现代码:

Interface .h:

NSArray *followingFriendsArray;
NSArray *followerFriendsArray;

@property (nonatomic, retain) NSArray *followingFriendsArray;
@property (nonatomic, retain) NSArray *followerFriendsArray;

Implementation .m:

- (void)handlerGetFollowingInformation:(id)value {
    BOOL success = [Utility checkWebServiceErrors:value controller:self.navigationController];

    if (success) {
        Friend *friend = (Friend *)value;

        followingFriendsArray = [[NSArray alloc] initWithArray:friend.Following];
        followerFriendsArray = [[NSArray alloc] initWithArray:friend.Followers];
    }
}

这就是我需要访问数组的方式:

- (void)followersButtonTapped:(id)sender {
    FollowingVC *fvc = [[FollowingVC alloc] initWithNibName:@"FollowingViewController" bundle:nil];

    fvc.friends = followerFriendsArray;

    [self.navigationController pushViewController:fvc animated:YES];
    [fvc release];
}

我按照通常的方式按以下方式释放我的两个 ivars:

- (void)viewDidUnload {
    self.followingFriendsArray = nil;
    self.followerFriendsArray = nil;
    [super viewDidUnload];
}

- (void)dealloc {
    [followingFriendsArray release];
    [followerFriendsArray release];
    [super dealloc];
}

我的意思是代码工作得很好,它是只是当我运行“Leaks”性能工具时,我担心所说的内存泄漏。

I am using the code below to set my two NSArray ivars:

The issue is, I keep getting a memory leak on the following lines:

followingFriendsArray = [[NSArray alloc] initWithArray:friend.Following];
followerFriendsArray = [[NSArray alloc] initWithArray:friend.Followers];

Is this not the correct way to set ivars from an existing NSArray of items? Any help would be appreciated. I've also tried to autorelease the above two lines, but when I actually access them in another method I get an error that they've already been released.

I have included my Interface and Implementation code below:

Interface .h:

NSArray *followingFriendsArray;
NSArray *followerFriendsArray;

@property (nonatomic, retain) NSArray *followingFriendsArray;
@property (nonatomic, retain) NSArray *followerFriendsArray;

Implementation .m:

- (void)handlerGetFollowingInformation:(id)value {
    BOOL success = [Utility checkWebServiceErrors:value controller:self.navigationController];

    if (success) {
        Friend *friend = (Friend *)value;

        followingFriendsArray = [[NSArray alloc] initWithArray:friend.Following];
        followerFriendsArray = [[NSArray alloc] initWithArray:friend.Followers];
    }
}

This is how I need to access the arrays:

- (void)followersButtonTapped:(id)sender {
    FollowingVC *fvc = [[FollowingVC alloc] initWithNibName:@"FollowingViewController" bundle:nil];

    fvc.friends = followerFriendsArray;

    [self.navigationController pushViewController:fvc animated:YES];
    [fvc release];
}

I release my two ivars in the following way as per usual:

- (void)viewDidUnload {
    self.followingFriendsArray = nil;
    self.followerFriendsArray = nil;
    [super viewDidUnload];
}

- (void)dealloc {
    [followingFriendsArray release];
    [followerFriendsArray release];
    [super dealloc];
}

I mean the code works just fine, it's just that I'm concerned about said memory leaks when I run the "Leaks" performance tool.

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

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

发布评论

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

评论(3

怀中猫帐中妖 2024-10-24 10:31:37

好吧,

在这种情况下,您不应该使用 autorelease,但是您必须通过调用 : 来释放数组:

[followingFriendsArray release];
[followerFriendsArray release];

您可以这样做:

  1. 当您不再需要使用它们时。
  2. 在 .m 文件的 dealloc 方法中。

选项 2 看起来像那样 -

- (void)dealloc {
[followingFriendsArray release];
[followerFriendsArray release];
[super dealloc];
}

顺便说一句 -
如果您在创建数组后不对其进行操作(添加/删除对象),则应该使用不可变数组(NSArray)。
祝你好运

OK

you should not use autorelease in this case, but you have to release the arrays by calling :

[followingFriendsArray release];
[followerFriendsArray release];

you can do it:

  1. when you don't need to use them any more.
  2. in the dealloc method in your .m file.

option 2looks like that -

- (void)dealloc {
[followingFriendsArray release];
[followerFriendsArray release];
[super dealloc];
}

BTW -
if you don't manipulate the arrays after creating them (add / remove objects) you should use an immutable array (NSArray).
Good Luck

无远思近则忧 2024-10-24 10:31:37

您的方法 handlerGetFollowingInformation 正在为 followingFriendsArrayfollowerFriendsArray 分配新值,而不释放以前的内容。如果您在同一个实例上多次调用此方法,则会发生泄漏。

Your method handlerGetFollowingInformation is assigning new values to followingFriendsArray and followerFriendsArray without releasing the previous contents. If you call this method more than once on the same instance you will leak.

那片花海 2024-10-24 10:31:37

CRD 是正确的,数组不会在 handlerGeFollowingInformation 方法内释放,但修复可能有点过头了。您需要做的是使用 self. 以便调用 setter 方法来自动执行此操作。你应该看起来像这样:

- (void)handlerGetFollowingInformation:(id)value {
    BOOL success = [Utility checkWebServiceErrors:value controller:self.navigationController];

    if (success) {
        Friend *friend = (Friend *)value;

        self.followingFriendsArray = [[NSArray alloc] initWithArray:friend.Following];
        self.followerFriendsArray = [[NSArray alloc] initWithArray:friend.Followers];
    }
}

容易修复但很难发现,我一遍又一遍地遇到这个问题,特别是当我开始释放属性时。

-安迪

CRD is right that the arrays are not released inside the handlerGeFollowingInformation method but the fix is maybe overkill. What you need to do is to use self. so that the setter method is called which does that automatically. You could should look like this:

- (void)handlerGetFollowingInformation:(id)value {
    BOOL success = [Utility checkWebServiceErrors:value controller:self.navigationController];

    if (success) {
        Friend *friend = (Friend *)value;

        self.followingFriendsArray = [[NSArray alloc] initWithArray:friend.Following];
        self.followerFriendsArray = [[NSArray alloc] initWithArray:friend.Followers];
    }
}

Easy fix but hard to spot and I ran into this issue over and over again especially when I started to dealloc are the properties.

-Andy

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