Tutorial :Memory over-release problem when I am animating UIView



Question:

I have enabled NSZombie's and I am getting the following message in my console when I am running my application:

 *** -[UIViewAnimationState release]: message sent to deallocated instance 0xf96d7e0  

Here is the method that is performing the animation

-(void)loadAvatar:(STObject*)st  {         NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];            avatar.alpha = 0;      avatar.frame = avatarRectSmall;        avatar.image = [ImageCache getMemoryCachedImageAtUrl:st.avatar_url];        [UIView beginAnimations:nil context:nil];      [UIView setAnimationDuration:.50];        avatar.frame = avatarRectNormal;      [avatar setAlpha:1];      [UIView commitAnimations];          [pool release];      pool = nil;  }  

I don't always get a crash, only sometimes. I'm wondering what is getting released?


Solution:1

You have an autorelease pool there which prompts me to ask, is this a separate thread? If the answer is yes then you can't do stuff to UIView there. UIKit is not thread safe. You can do other things like calculating positions or updating images which you later put on the screen but any user interface stuff has to happen in the main thread.

Graphics and Drawing section of iPhone Application Programming Guide


Solution:2

You can use very simple security check fof all functions doing something with UI:

-(void)functionModifyingUIelements:(id)object  {   // fire itself in main thread if it is not in it already   if (![[NSThread currentThread] isMainThread]) {             [self performSelectorOnMainThread:@selector(functionModifyingUIelements:) withObject:object waitUntilDone:NO];          return;      }    }  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »