I have a dictionary that’s created and initialized in following ways
NSDictionary *dict = [[NSMutableDictionary alloc] initWithCapacity:N];
for (int index = 0; index < N; index++) {
MyObject *obj = [[MyObject alloc] init];
...
[dict setObject:obj forKey:index];
}
So the questions are
1. should the MyObject instances be created as autorelease?
2. once I need to release the NSDictionary instance, should I also release all the MyObject instances (if they are not created as autorelease)
You can explicitly release MyObject instances after calling setObject since they will be retained at that point. You can autorelease MyObject instances, but explicit releasing will be more efficient. Releasing the NSDictionary instance will release MyObject instances.