ios - Objective-C should I set Arrays to nil after use? -


is practice dealloc items i've created inside method or class after use?

for instance, if have 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]];      (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 set classes nil? dealloc it? happen automatically?

the thing realise when set object nil, setting piece of memory set object nil, set of bits - else. so, setting object nil won't cause object dealloc'd. object should released memory when no longer has references it, or go out of scope - example, method defined in has completed , returned.

you can dealloc object calling release method on it. means next time system comes around see object has been released , clear memory (in simple terms...) other option call autorelease on object. useful in situations when aren't finished object, don't know when finished, , don't know object end - example, object representing piece of data of server might passed around controller controller , view view. using autorelease ensures @ point memory free'd up.

as can see, becomes tricky mess keeping track of state of every object is, along comes arc (automatic reference counting). looks after of you, assuming enabled.

in case, if have arc on code tip-top in terms of memory management. if not, there bunch of things, need handled autoreleasing copy of array return. suggest take @ document gain more understanding on memory management in ios environment - in long run when confusing memory based errors @ run-time.

https://developer.apple.com/library/mac/documentation/cocoa/conceptual/memorymgmt/articles/memorymgmt.html

hope helps.


Comments

Popular posts from this blog

c++ - How to add Crypto++ library to Qt project -

jQuery Mobile app not scrolling in Firefox -

How to use vim as editor in Matlab GUI -