I am facing memory leak issue with very simple code.I have class ‘TestClass’
@interface TestClass : NSObject
@property(nonatomic,retain) NSString *name;
@end
its implementation is like this:
@implementation TestClass
@synthesize name;
-(id)init {
if (self = [super init]) {
self.name = @"";
}
return self;
}
-(void) dealloc
{
[name release];
name = nil;
}
@end
There is another view controller inside viewWillAppear i am creating object and releasing immediately as follows
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
for (int i =0; i<50; i++) {
TestClass *testClass = [[TestClass alloc] init] ;
[testClass release];
}
}
The leak instrument shows memory leak on line TestClass *testClass = [[TestClass alloc] init] ; while if i remove init and dealloc method from TestClass there is no memory.
Thanks in advance.
It’s because you don’t release the TestClass in the dealloc of its own implementation of dealloc:
Also as a tip, I would suggest moving way from memory management code and moving to
ARC.