ios - How do I solve this Automatic Reference Counting (ARC) conflict? -
i have uiviewcontroller
requires facebook login button present in screen. now, facebook ios button requires arc off.
on other hand, in same uiviewcontroller
using nstimer
show few photos slideshow @ background - feature requires arc setting.
so, have single file requires arc 1 of components, while not other.
the exact problem in code:
-(void)handletimer { [uiview transitionwithview:imageview duration:3 options:(uiviewanimationoptiontransitioncrossdissolve | uiviewanimationoptioncurveeaseinout) animations:^{ imageview.image = [uiimage imagenamed:[myimages objectatindex:imageptr]]; } completion:nil]; if(imageptr == (myimages.count) - 1) imageptr = 0; else imageptr = imageptr + 1; }
if disable arc file has code, throws error @ line:
imageview.image = [uiimage imagenamed:[myimages objectatindex:imageptr]];
the error reads:
thread 1 : exc_bad_access (code=exc_i386_gpflt)
actually, have time see above, continuously update image in imageview.
myimageview of type
@property (nonatomic, assign) iboutlet uiimageview *imageview;
so, problem here?
here how myimages initialized in viewdidload() method:
@interface viewcontroller () { uiimage *nextimage; nsarray *myimages; int imageptr; } @end @implementation viewcontroller @synthesize imageview; - (void)viewdidload { [super viewdidload]; myimages = @[@"image1.jpg", @"image2.jpg", @"image3.jpg", @"image4.jpg", @"image5.jpg", @"image6.jpg", @"image7.jpg", @"image8.jpg", @"image9.jpg", @"image10.jpg", @"image11.jpg", @"image12.jpg", @"image13.jpg", @"image14.jpg", @"image15.jpg", @"image16.jpg", @"image17.jpg", @"image18.jpg", @"image19.jpg", ]; imageptr = 0; nstimer *timer; imageview.image = [uiimage imagenamed:[myimages objectatindex:imageptr]]; imageptr = imageptr + 1; //imageview.image = [uiimage imagenamed:[myimages objectatindex:imageptr]]; timer = [nstimer scheduledtimerwithtimeinterval: 3 target: self selector: @selector(handletimer) userinfo: nil repeats: yes]; // additional setup after loading view, typically nib. }
your controller compiled under mrc. means ivars doesn't retain values assign them, example:
myimages = @[...];
will assign pointer array released anyway , when try access myimages
later, application crash.
solutions:
mrc -
retain
when assigning ivars, retain value explicitly (myimages = [@[...] retain];
), don't forgetrelease
in-dealloc
.mrc - properties
remove ivars , declare them properties@property (nonatomic, retain) nsarray *myimages;
... self.myimages = @[...];
again, don't forget release array in-dealloc
arc (the preferred solution) - compile controller arc , values in ivars implicitly
__strong
, retained you.
Comments
Post a Comment