iPhone - Objective-C - initWithArray 内存泄漏
我正在使用下面的代码来设置我的两个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好吧,
在这种情况下,您不应该使用 autorelease,但是您必须通过调用 : 来释放数组:
您可以这样做:
选项 2 看起来像那样 -
顺便说一句 -
如果您在创建数组后不对其进行操作(添加/删除对象),则应该使用不可变数组(NSArray)。
祝你好运
OK
you should not use autorelease in this case, but you have to release the arrays by calling :
you can do it:
option 2looks like that -
BTW -
if you don't manipulate the arrays after creating them (add / remove objects) you should use an immutable array (NSArray).
Good Luck
您的方法
handlerGetFollowingInformation
正在为followingFriendsArray
和followerFriendsArray
分配新值,而不释放以前的内容。如果您在同一个实例上多次调用此方法,则会发生泄漏。Your method
handlerGetFollowingInformation
is assigning new values tofollowingFriendsArray
andfollowerFriendsArray
without releasing the previous contents. If you call this method more than once on the same instance you will leak.CRD 是正确的,数组不会在 handlerGeFollowingInformation 方法内释放,但修复可能有点过头了。您需要做的是使用 self. 以便调用 setter 方法来自动执行此操作。你应该看起来像这样:
容易修复但很难发现,我一遍又一遍地遇到这个问题,特别是当我开始释放属性时。
-安迪
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:
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