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;
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!!!
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];
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.