Reputation: 986
I have a class called CategoryViewController and its viewDidLoad method calls this method:
- (void)reset {
Category * c = [[Category alloc] initWithId:0 title:@"Categories"];
[self setCategory:c];
[c release]; // <--- line of code I am interested in
self.title = @"Categories";
[self fillCategory];
}
In most situations category here would be null, but sometimes reset needs to be called after category has been assigned. Without the line I marked in my code, the program builds and debugs just fine and I can pull up instruments and check my leaks. The only leak I can find is a category initialized from this function (because without the release, I get a leak when calling this function on a CategoryViewController that has already been initialized).
If I try to run this method as is WITH the release on c, Instruments, XCode, and the Simulator all begin to act strangely, crashing and freezing, giving me random SIGABRTs and SIGKILLs. I can build and debug with the line of code in there, but Instruments won't even start my app. Can anyone give me a hint as to what's going on here?
EDIT: More code
@implementation Category
@synthesize title, articleCount, seeAlso, categoryId, articles, subcategories;
- (id)initWithId:(NSInteger)cid title:(NSString*)t{
self.title = t;
self.categoryId = cid;
[self setArticles:[[NSMutableArray alloc] init]];
[self setSubcategories:[[NSMutableArray alloc] init]];
[self setSeeAlso:[[NSMutableArray alloc] init]];
self.articleCount = 0;
return self;
}
Upvotes: 0
Views: 177
Reputation: 986
It's funny how these things seem to be resolved so easily after you take the time to post them online. After posting the Category init code I realized I wasn't properly releasing the allocations I made. My leaks as well as my crashes appear to be gone after proper memory management like so:
- (id)initWithId:(NSInteger)cid title:(NSString*)t{
self.title = t;
self.categoryId = cid;
NSMutableArray * m = [[NSMutableArray alloc] init];
[self setArticles:m];
[m release];
m = [[NSMutableArray alloc] init];
[self setSubcategories:m];
[m release];
m = [[NSMutableArray alloc] init];
[self setSeeAlso:m];
[m release];
self.articleCount = 0;
return self;
}
Upvotes: 1
Reputation: 21736
Assuming your property is declared like below, I don't see any problem with the line you highlighted, nor with the rest of your code:
@property (retain) Category *c;
Just shooting in the dark, but last time Xcode, Instruments and the Simulator did crazy stuff, I had an infinite recursion going on.
Are you sure your method fillCategory
doesn't somehow call your viewDidLoad
or reset
method?
What happens when you set a breakpoint on the first line of your reset
method, and follow your code step-by-step?
Upvotes: 0