I have a class like this:
@interface MyCollection : NSObject {
NSMutableDictionary *data;
}
and in the implementation of it, I have a method to init it like this:
- (id) init {
if(self = [super init])
{
self.data = [[NSMutableDictionary alloc] init];
}
return self;
}
Now when I create a object of this class in my code like this:
MyCollection *c = [[MyCollection alloc] init];
… at which point the Leaks utility shows that I have a memory leak in the init function on the very line where I try to set up the instance variable. I am totally new to Objective C & Iphone and I can’t just get what is going wrong here. I have read through the Memory Management Guide and all, but I think I’m missing something pretty serious here.
Any help would be greatly appreciated. Thanks for your time already.
you are using
self.data =. So there is most likely a property. And it most likely is a property which either copies or retains your object if you use it.By calling
The retain count of the NSMutableDictionary increases because of the alloc, and if the property of data has a retain or copy statement you get another increase in retain count.
you could write
data = [[NSMutableDictionary alloc] init];orself.data = [NSMutableDictionary dictionary]. This would increase the retain count only one time.And don’t forget to release the object in dealloc.