how to use correctly retain and release in an app for iPhone / iPad
I'm a newbie in programming for iPhone / iPad with Objective-C.
I have problems with memory management by releasing or retaing objects. I'm doing an application that contains multiple view controllers for switching views.
The most of the objects that use are declared as IBOutlets and I would like to know how and when should I use the Retain and release to not have memory problems and ending observing proper application.
It would be interesting for me to release them when switching to another view controller or whem I don't have to use more times an object.
Can someone explain me how and when I must use the Retain and release? I've read Apple's documentation but I think that it's a bit confusing. I've that I must put 开发者_如何转开发there in dealloc or didReceiveMemoryWarning, but does not solve my problem.
I'm sorry if I made spelling errors. Thanks in andance.
for finding leaks in your project run the application with instrumentation tool or simple run the app from xcode by shift+command+A.
every object that you are used alloc and init should be released else it'll cause memory leak. declare every object in .h file and set the property and release that obj in dealloc method.
retaining is only require if you have to retain the present state after u left from the page.
If you send copy
mutablecopy
retain
or alloc
to an Object then you're responsible to release that object when you're done with it.
NSString *allocedString = [[NSString alloc] initWithString:@"world"];
NSString *myString = [NSString stringWithFormat:@"Hello%@", allocedString];
[allocedString release], allocedString = nil;
You are responsible for releasing allocedString
but not myString
(which uses autorelease
inernally)
精彩评论