tags:

views:

26

answers:

2

i am using this a1 = [[NSMutableArray alloc] init]; now what should be the best way to release a1

right now i am releasing in dealloc()

i am allocation a1 in viewdidload() and displaying 10 images there

A: 

A basic example would be this:

// get the party started
NSMutableArray *tmpArray = [[NSMutableArray alloc] init];
// as an example, just copy some user defaults into the mutable array
tmpArray = [[userDefaults objectForKey:@"UserDefaultsExample"] mutableCopy];

// do something here with tmpArray (i.e. if ([tmpArray count] == 0) //do something)

// once you are done with tmpArray, go ahead and release it
[tmpArray release];

For your case, you'd want to release the array once you are finished with it (i.e. at the end of viewDidLoad is probably a safe bet as long as you truly are finished with it). If you do it this way, you don't need to create an NSMutableArray object in your .h file, synthesize it and release in dealloc. Instead, you just create a temporary one like I did above and release it when you're done showing the images from it.

iWasRobbed
thanks a loteven if i release my reatincount =1[a1 release]; NSLog(@"what youdont have %d",[a1 retainCount]);=1how to make it 0 and is it neceaasry to trace all arrays and string reatinCount =0 for memory optimization??
ram
Using a convenience method like `mutableCopy` puts the object into an autorelease pool for you so your retain count likely won't be zero right away. You may want some additional reading: http://cocoawithlove.com/2009/07/rules-to-avoid-retain-cycles.html and http://developer.apple.com/mac/library/DOCUMENTATION/Cocoa/Conceptual/MemoryMgmt/MemoryMgmt.html
iWasRobbed
Thanks a lot :-)
ram
hey key like thi UserDefaultsExample can by anything right??? its just a text isnt it ??? or what else .. are you using UserDefaultsExample any wer other then tmpArray = [[userDefaults objectForKey:@"UserDefaultsExample"] mutableCopy];
ram
Read the 3rd paragraph of the Overview section for what data types it accepts: http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSUserDefaults_Class/Reference/Reference.html ..User defaults work via property lists so it has to be in that format.
iWasRobbed