内存管理:添加到 NSMutableArray 后释放 viewcontroller 对象
我有以下代码的内存泄漏。如果我使用 [sub release];
将 sub 添加到 NSmutableArray(subViewController) 后,分析器会显示“调用者此时不拥有的对象的引用计数的不正确递减” ,当我删除 [sub release]
时,它会显示“在 xx 行分配的对象的潜在泄漏”
for (int i=0; i<[self.data count]; i++) {
SubCategoryViewController *sub =[[SubCategoryViewController alloc]
initwithServiceUrl:urlString andHeaderValue:
((PMCategory *)[self.data objectAtIndex:i]).categoryName];
[[AppDelegate sharedAppDelegate].viewController.subViewControllers addObject:sub];
[sub release];
}
另外,如果我使用 autorelease,警告将变为 “对象已发送-autorelease 太多次”
SubCategoryViewController *sub =[[[SubCategoryViewController alloc]
initwithServiceUrl:urlString andHeaderValue:
((PMCategory *)[self.data objectAtIndex:i]).categoryName]autorelease];
从评论中添加: SubCategoryViewController 初始化方法:
@property (nonatomic, retain) NSMutableArray *data;
@property (nonatomic, copy) NSString *headerText;
@synthesize data = _data;
@synthesize headerText=_headerText;
...
self = [super init];
if (self) {
self.data = [[NSMutableArray alloc] init] ;
self.headerText =headerValue;
self.serviceURL =serviceU;
self.firstLoad = YES;
}
return self;
I have a memory leak with below code. If I use [sub release];
after adding sub to NSmutableArray(subViewController), Analyzer says "Inconrrect decrement of the reference count of an object that is not owned at this point by the caller", when i remove [sub release]
then it says "Potential leak of an object allocated on xx line"
for (int i=0; i<[self.data count]; i++) {
SubCategoryViewController *sub =[[SubCategoryViewController alloc]
initwithServiceUrl:urlString andHeaderValue:
((PMCategory *)[self.data objectAtIndex:i]).categoryName];
[[AppDelegate sharedAppDelegate].viewController.subViewControllers addObject:sub];
[sub release];
}
Alson if I use autorelease Warning becomes "Object sent -autorelease too many times"
SubCategoryViewController *sub =[[[SubCategoryViewController alloc]
initwithServiceUrl:urlString andHeaderValue:
((PMCategory *)[self.data objectAtIndex:i]).categoryName]autorelease];
Added from comment:
SubCategoryViewController Init method:
@property (nonatomic, retain) NSMutableArray *data;
@property (nonatomic, copy) NSString *headerText;
@synthesize data = _data;
@synthesize headerText=_headerText;
...
self = [super init];
if (self) {
self.data = [[NSMutableArray alloc] init] ;
self.headerText =headerValue;
self.serviceURL =serviceU;
self.firstLoad = YES;
}
return self;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是因为你没有遵循 Objective c 的正确命名约定。每当你编写任何初始化函数时,命名约定就像是
在 init 大写后面加上第一个字母。
因此,将您的
initwithServiceUrl
更改为initWithServiceUrl
,您的问题将得到解决。开心点!!!
it is because you haven't followed proper naming convention of Objective c. Whenever you are writing any initialization function namingConvention is something like
means make first letter after init Capital.
So change your
initwithServiceUrl
toinitWithServiceUrl
and your problem will be solved.cheer up!!!
如果
data
是具有retain
属性的属性,则它会被过度保留,一次由:,一次由属性设置者。
将其更改为使用返回自动释放的 NSMutableArray 的类便捷方法:
此外,可以通过快速枚举更清晰地编写 for 循环:
If
data
is a property with theretain
attribute then it is over retained, once by:and once by the property setter.
Change it to use the class convenience method that returns an autoreleased
NSMutableArray
:Additionally the for loop can be written more clearly with fast enumeration: