Reputation: 1205
Do I have to release the returnSet variable ?
NSMutableSet* returnSet = [[NSMutableSet alloc] init];
for (Information* currentInformation in self.information) {
if ([currentInformation.player isEqual:aPlayer]) {
[returnSet addObject:currentInformation];
}
}
return [NSSet setWithSet:returnSet];
Thanks for your answers, Christian
Upvotes: 0
Views: 794
Reputation: 3772
Generally when writing a method that returns a newly created object (as in your example), you should return an auto-released object. So, to follow convention, your code would become:
NSMutableSet* returnSet = [[NSMutableSet alloc] init];
for (Information* currentInformation in self.information) {
if ([currentInformation.player isEqual:aPlayer]) {
[returnSet addObject:currentInformation];
}
}
return [returnSet autorelease];
Note that you can return your mutable set from the method even if your method signature specifies a NSSet, since an NSMutableSet is a subclass of NSSet. When you use this method, if you don't want the returned object to stick around, just do nothing and it will get deallocated. If you want it to be accessible later, assign it to a member variable and retain it, or place it inside another retained data structure (set, dictionary, array).
Update To clear up some apparent confusion as to the correctness of this answer, refer to the "Returning Objects from Methods" section at http://developer.apple.com/library/ios/#documentation/cocoa/Conceptual/MemoryMgmt/Articles/mmPractical.html.
Upvotes: 4
Reputation: 38485
@bensnider is absolutely correct - return an autoreleased object.
However, I personally prefer to return an immutable set to prevent accidents later on :)
I would be tempted to write this :
NSMutableSet *returnSet = [[NSMutableSet set];
for (Information* currentInformation in self.information) {
if ([currentInformation.player isEqual:aPlayer]) {
[returnSet addObject:currentInformation];
}
}
return [NSSet setWithSet:returnSet];
Then resultSet is created with an implicit autorelease so you don't need to worry about it and you are returning an immutable object.
(though the downside of this is you are making two sets - probably not an issue but if this method is called lots of times you might want to rethink my answer!)
Upvotes: 0
Reputation: 9544
Yes you do. You can do a simple autorelease like this:
[returnSet autorelease];
return returnSet;
Using autorelease gives you time to assign the returned value to something else in the function that called this. Releasing right away would dealloc your object before the calling function could do anything with the return. Its also good practice to handle the autorelease in this function since it was created in this function. It makes life much easier instead of having to remember to release every time you call this function.
Upvotes: 0
Reputation: 6413
Yes. It's memory leak. You need to create NSSet result variable, release returnSet and return results.
Upvotes: 1
Reputation: 19867
Yes. In general, if you alloc
it, you either need to release
it or autorelease
it.
Upvotes: 3