Code in my controller:
CalcBorderBlocks *calcBB = [[CalcBorderBlocks alloc] init];
NSMutableArray *blockArray = [calcBB calc:341241133];
[calcBB release]; // Releases calcBB.
Code in CalcBorderBlocks.h:
#import <Foundation/Foundation.h>
@interface CalcBorderBlocks : NSObject {
@private
NSMutableArray *blockArray_;
}
@property(nonatomic, retain) NSMutableArray *blockArray;
- (NSMutableArray *)calc:(int)blockID;
@end
Code in CalcBorderBlocks.m:
#import "CalcBorderBlocks.h"
@implementation CalcBorderBlocks
@synthesize blockArray = blockArray_;
- (id)init {
self = [super init];
if (self) {
blockArray_ = [[NSMutableArray alloc] init]; // Retain count should be 1
}
return self;
}
- (NSMutableArray *)calc:(int)blockID {
// Do stuff
return self.blockArray;
}
- (void)dealloc {
[blockArray_ release]; // Normal cleanup, yet crashes! Works fine if I comment out this line.
[super dealloc];
}
@end
If you’re doing anything with
blockArrayafter this line:Then that’s the cause of the crash.
You’re releasing
calcBBwhich in turn releasesblockArrayin dealloc.I suspect you need to retain blockArray, process it as required then release it afterwards.