Pregunta

I use UIAlertview to input some text, and it works fine. However, after the alertview is closed, there is still more than 17MB increase in memory before alertview is opened(from 9MB to 26 MB). I used Instrument Allocation to measure heap memory and activity monitor to measure it. Both of them show the similar result. When I switch to other App, the memory increased by alertview will be reduced to the proper level. Is it normal ? My environment is iPhone4+iOS6. Here's some code for trying.

   UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"input caption" message:@"text"
                                                   delegate:self cancelButtonTitle:@"cancel"  otherButtonTitles:@"OK", nil];
   [alert show];
   [alert release];

Thank you.


Dear Naveed S and Purr, Thank you very much. I'm not sure I follow your suggestion correctly. But I use Activity monitor watch the Real mem, the memory still increase about 20 MB after alertview is closed. I remove [alert release] after [alert show], and I wrote

  [alert show];
 //[alert release];

 - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
 { [alertView dismissWithClickedButtonIndex:buttonIndex animated:YES];
  [alertView autorelease];                                                      }      

Moreovre, the memory is reduced when the app enter background mode after I press home. (This is similar as my previous code). How could I watch the abandon memory correctly. I'm even not sure how to watch it in the right way.....

BTW, my original codes follow the post here. Uialertview and memory management

¿Fue útil?

Solución

For one thing, you should autorelease your alertView. -alertView:didDismissWithButtonIndex: is called by the UIAlertView, so the alertView object should still be valid (not -release'd) when your method returns.

Better yet, convert to ARC and be done with manual -retain, -release and -autorelease ;)

If you're not converting to ARC, do this: remove the -release from the code in your original post, uncomment the //CRASH in your -alertView:didDismissWithButtonIndex: method and change the -release to -autorelease there.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top