Reputation: 951
Is it good practice to dealloc any items I've created inside a method or class after use?
For instance, if I have this method:
-(NSArray *)splitClassesWithData:(NSData *)data {
NSString *htmlString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSMutableArray *classes = [[NSMutableArray alloc] init];
NSUInteger length = [htmlString length];
NSRange range = NSMakeRange(0, length);
while(range.location != NSNotFound)
{
range = [htmlString rangeOfString: @"lblperiod" options:0 range:range];
if(range.location != NSNotFound)
{
range = NSMakeRange(range.location + range.length, length - (range.location + range.length));
NSNumber *toAdd = [NSNumber numberWithInteger:range.location];
[classes addObject:toAdd];
}
}
int counter = 0;
NSNumber *count = [NSNumber numberWithInteger:[classes count] - 1];
NSMutableArray *classArray = [NSMutableArray arrayWithCapacity:[classes count]];
for (NSNumber *i in classes) {
int loc = [[classes objectAtIndex:counter] intValue];
int len = -loc;
if ([count intValue] > [[NSNumber numberWithInt:(counter)] intValue]) {
NSRange range = NSMakeRange(loc, (len + [[classes objectAtIndex:(counter+1)] intValue]));
NSString *rangeString = [htmlString substringWithRange:range];
if ([rangeString rangeOfString:@"Academic Adviso"].location == NSNotFound) {
[classArray addObject:htmlString];
} else {
NSLog(@"disinclude AA");
}
} else {
NSRange range = NSMakeRange(loc, ([htmlString length] + len));
htmlString = [htmlString substringWithRange:range];
[classArray addObject:htmlString];
}
counter++;
}
return [classArray copy];
}
Should I set classes
to nil? Would that dealloc it? Does it just happen automatically?
Upvotes: 1
Views: 650
Reputation: 200
The thing to realise is that when you set the object to nil, you are just setting the piece of memory set for that object to nil, which is just a set of bits - like anything else. So, setting an object nil won't cause the object to be dealloc'd. An object should be released from memory when it no longer has any references to it, or it is about to go out of scope - for example, the method it was defined in has completed and returned.
You can dealloc an object by calling the release method on it. This means that the next time the system comes around it will see the object has been released and clear that memory up (in simple terms...) The other option is to call autorelease on an object. This is useful in situations when you aren't finished with the object, but don't know when you will be finished, and don't know where the object will end up - for example, an object representing a piece of data of a server might get passed around from controller to controller and view to view. Using autorelease ensures that at some point that memory will be free'd up.
As you can see, it becomes a tricky mess keeping track of what the state of every object is, so along comes ARC (automatic reference counting). Basically it looks after all of it for you, assuming it is enabled.
In your case, if you have ARC on then your code is tip-top in terms of memory management. If not, there are a bunch of things, that need to be handled like autoreleasing the copy of the array you return. I suggest you take a look at this document to gain more understanding on memory management in the iOS environment - it will help you in the long run when you get confusing memory based errors at run-time.
Hope it helps.
Upvotes: 1
Reputation: 13302
If you use Automatic Reference Counting then your code is correct and compiler automatically inserts retain/release/autorelease calls.
But you are not using ARC then you should do it manually. In your case:
- (NSArray *)splitClassesWithData:(NSData *)data {
// ...
// Main method logic
// ...
[classes release];
return [[classArray copy] autorelease];
Upvotes: 0
Reputation: 2195
If you are using ARC (and I'm pretty sure you are), then there's no need to nil it. It will fall out of scope, there will be no reference to the array, and its memory will be freed up
Upvotes: 0