内存管理:添加到 NSMutableArray 后释放 viewcontroller 对象

发布于 2024-12-28 22:16:46 字数 1342 浏览 1 评论 0原文

我有以下代码的内存泄漏。如果我使用 [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 技术交流群。

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

发布评论

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

评论(2

甜警司 2025-01-04 22:16:46

这是因为你没有遵循 Objective c 的正确命名约定。每当你编写任何初始化函数时,命名约定就像是

-(id) initFirstWordSecondWord{
}

在 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

-(id) initFirstWordSecondWord{
}

means make first letter after init Capital.

So change your initwithServiceUrl to initWithServiceUrl and your problem will be solved.
cheer up!!!

任性一次 2025-01-04 22:16:46

如果 data 是具有 retain 属性的属性,则它会被过度保留,一次由:

[[NSMutableArray alloc] init]

,一次由属性设置者。

将其更改为使用返回自动释放的 NSMutableArray 的类便捷方法:

self.data = [NSMutableArray array];

此外,可以通过快速枚举更清晰地编写 for 循环:

for (PMCategory *category in self.data) {
    SubCategoryViewController *sub =[[SubCategoryViewController alloc] 
        initwithServiceUrl:urlString
        andHeaderValue:category.categoryName];

If data is a property with the retain attribute then it is over retained, once by:

[[NSMutableArray alloc] init]

and once by the property setter.

Change it to use the class convenience method that returns an autoreleased NSMutableArray:

self.data = [NSMutableArray array];

Additionally the for loop can be written more clearly with fast enumeration:

for (PMCategory *category in self.data) {
    SubCategoryViewController *sub =[[SubCategoryViewController alloc] 
        initwithServiceUrl:urlString
        andHeaderValue:category.categoryName];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文