简体   繁体   中英

iPhone Memory Management

I have a problem with my code. I am running my application with performance tool so that i can see how much memory my code uses. When i used popViewController method of UINavigationController the allocated places doesn't seem to be released even I have released all the objects in viewDidUnload method using "self.myarray = nil;" "myarray" is synthesized in the implementation file. What can be the reason which causes this?

when an alertview is showed it increases the allocated memory. It is ok. But even if i tapped the ok button, it does not release the allocated memory.

Sample code is here for AlertView part.

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Seri Seçilmedi"       
                                                message:@"Lütfen bir seri seçiniz."
                                               delegate:self 
                                      cancelButtonTitle:@"Tamam" 
                                      otherButtonTitles:nil];

[alert show];
[alert release];

viewDidUnload will be called in case of memory warnings, but not with popViewController. You should release the objects in the dealloc method.

This is the best way of releasing memory allocated for UIAlertViews. As far as I have used them, I have deallocated it in dealloc() as phix23 mentioned.

 if(self.alert.visible) {
            [self.alert dismissWithClickedButtonIndex:-1 animated:NO];
        }
  [alert release];
  self.alert = nil;
if(self.alert.visible) {
          [self.alert dismissWithClickedButtonIndex:-1 animated:NO];
}

[alert release];//If you only do [alert release], you will release the memory, but the alert still point to the memory
self.alert = nil; // alert no longer exists.

If you just release an object, then it will become freed object.

And if you perform any operation on freed object then your app crashes. To avoid it is always preferred "assign your object to nil after releasing it". Because we all know any operations performed on nil will not be executed :)

or you can set autorelease

UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"Seri Seçilmedi"       
                                            message:@"Lütfen bir seri seçiniz."
                                           delegate:self 
                                  cancelButtonTitle:@"Tamam" 
                                  otherButtonTitles:nil]autorelease];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM