Crashes on sizeWithFont and its replacement on iOS 7 - iphone

I have the application that uses sizeWithFont method. It crashes on about 5% of launches of the application on iOS 7. The method is deprecated on SDK 7, so I replaced it with the following category:
#import "NSString+mySizeWithFont.h"
#import "Constants.h"
#implementation NSString (mySizeWithFont)
- (CGSize)mySizeWithFont:(UIFont *)font {
if (is_iOS7) {
CGSize size = CGSizeMake(MAXFLOAT, MAXFLOAT);
return [self mySizeWithFont:font constrainedToSize:size];
} else {
return [self sizeWithFont:font];
}
}
- (CGSize)mySizeWithFont:(UIFont*)font constrainedToSize:(CGSize)size {
if (is_iOS7) {
NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:font, NSFontAttributeName, nil];
CGRect frame = [self boundingRectWithSize:size options:(NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading) attributes:attributesDictionary context:nil];
return frame.size;
} else {
return [self sizeWithFont:font constrainedToSize:size];
}
}
#end
But now I have another crashes on the same 5% of launches. There are crash reports of 2 types:
Exception Type: EXC_BAD_ACCESS (SIGSEGV)
Exception Subtype: KERN_INVALID_ADDRESS at 0x0005006f
Triggered by Thread: 0
Thread 0 Crashed:
0 libobjc.A.dylib 0x3902cb76 objc_msgSend + 22
1 CoreFoundation 0x2ec74c56 +[__NSDictionaryI __new:::::] + 358
2 CoreFoundation 0x2ec749c6 -[__NSPlaceholderDictionary initWithObjects:forKeys:count:] + 238
3 CoreFoundation 0x2ec794d4 +[NSDictionary dictionaryWithObjectsAndKeys:] + 372
4 *** MYAPP *** 0x000cd99e -[NSString(mySizeWithFont) mySizeWithFont:constrainedToSize:] (NSString+mySizeWithFont.m:25)
5 *** MYAPP *** 0x000cd7e6 -[NSString(mySizeWithFont) mySizeWithFont:] (NSString+mySizeWithFont.m:17)
6 *** MYAPP *** 0x000d36ae -[LiteVersionHomepageLink drawRect:] (LiteVersionHomepageLink.m:43)
And this one:
Exception Type: EXC_CRASH (SIGABRT)
Exception Codes: 0x0000000000000000, 0x0000000000000000
Triggered by Thread: 0
Last Exception Backtrace:
0 CoreFoundation 0x2ed36e86 __exceptionPreprocess + 126
1 libobjc.A.dylib 0x390316c2 objc_exception_throw + 34
2 CoreFoundation 0x2ed3a7b2 -[NSObject(NSObject) doesNotRecognizeSelector:] + 198
3 CoreFoundation 0x2ed390b2 ___forwarding___ + 702
4 CoreFoundation 0x2ec87e94 __forwarding_prep_0___ + 20
5 UIFoundation 0x3660790e __NSStringDrawingEngine + 2950
6 UIFoundation 0x36606d5a -[NSString(NSExtendedStringDrawing) boundingRectWithSize:options:attributes:context:] + 130
7 *** MYAPP *** 0x000e89d4 -[NSString(mySizeWithFont) mySizeWithFont:constrainedToSize:] (NSString+mySizeWithFont.m:26)
8 *** MYAPP *** 0x000e87e6 -[NSString(mySizeWithFont) mySizeWithFont:] (NSString+mySizeWithFont.m:17)
9 *** MYAPP *** 0x000ee6ae -[LiteVersionHomepageLink drawRect:] (LiteVersionHomepageLink.m:43)
LiteVersionHomepageLink is inherited from UIView:
#interface LiteVersionHomepageLink : UIView {
NSString *text;
UIFont *textFont;
}
And mySizeWithFont is called from the following method:
- (void)drawRect:(CGRect)rect {
[[UIImage imageNamed:#"cal_top_back#2x"] drawInRect:rect];
if (text && textFont) {
[[UIColor whiteColor] set];
float height = [text mySizeWithFont:textFont].height / 2;
[text drawInRect:CGRectMake(0, rect.size.height / 2 - height, rect.size.width, rect.size.height / 2 + height) withFont:textFont lineBreakMode:NSLineBreakByCharWrapping alignment:NSTextAlignmentCenter];
}
}
What's wrong? How to fix crashes? Thanks a lot for any help!

It looks to me like you're calling your routine mySizeWithFont with an invalid font. Either one that has been released, or one that was never allocated. I'd put in NSLog breadcrumbs near your font allocation and just before the breadcrumbs to make sure your font is allocated first. If it is, it may be that ARC is tossing your font before you go to use it - may need #property(strong) or something like that.

My two cents: I had, in my app, an overrelease bug with a UIFont object. I'd invoke [UIFont systemFontOfSize:] and store the result in a file-static variable without retaining.
Up until iOS 7, I'd get away with it, then crashes in [sizeWithFont] started coming. Looks like iOS<7 would return font instances from a pool or a cache, so that a single unmatched release won't deallocate them. In iOS 7 - not anymore.

Related

Core Animation Warning: "uncommitted CATransaction"

I am getting the following warning with Device only:
CoreAnimation: warning, deleted thread with uncommitted CATransaction; created by:
0 QuartzCore 0x3763c65d <redacted> + 220
1 QuartzCore 0x3763c541 <redacted> + 224
2 QuartzCore 0x3763fe2f <redacted> + 30
3 QuartzCore 0x3763b9bf <redacted> + 318
4 QuartzCore 0x3763b87b <redacted> + 50
5 QuartzCore 0x3763b80b <redacted> + 538
6 MyApp 0x000df597 -[CommonClass orangeGradient:] + 382
7 MyApp 0x000f70e1 -[HomeViewController updateStatusBar] + 1700
8 MyApp 0x000f61bd -[HomeViewController statusRecieved] + 224
9 MyApp 0x000cd323 -[CommonClass statusReceivedFromServer] + 142
10 MyApp 0x000d833d -[CommonClass accountStatus] + 7416
11 Foundation 0x35a3767d <redacted> + 972
12 libsystem_c.dylib 0x35c9a311 <redacted> + 308
13 libsystem_c.dylib 0x35c9a1d8 thread_start + 8
My Method that is on top of the stack is as follows:
- (void)orangeGradient: (UILabel *)fillLabel {
#synchronized([CommonClass class]) {
CAGradientLayer * gradientLayer = [CAGradientLayer layer];
gradientLayer.frame = fillLabel.bounds;
gradientLayer.colors = [NSArray arrayWithObjects:(id)[UIColorFromRGB(0xfbb250) CGColor],(id)[UIColorFromRGB(0xf47c2a) CGColor], nil];
[fillLabel.layer addSublayer:gradientLayer];
}
}
Any idea on why is this coming, and how I can fix this?
It looks like orangeGradient: is called from a background thread. Core Animation groups all changes into CATransactions. Usually this is done automatically from the run loop. On background threads there is (usually) no run loop, so you have to create the transaction yourself:
- (void)orangeGradient: (UILabel *)fillLabel {
#synchronized([CommonClass class]) {
[CATransaction begin];
CAGradientLayer * gradientLayer = [CAGradientLayer layer];
gradientLayer.frame = fillLabel.bounds;
gradientLayer.colors = [NSArray arrayWithObjects:(id)[UIColorFromRGB(0xfbb250) CGColor],(id)[UIColorFromRGB(0xf47c2a) CGColor], nil];
[fillLabel.layer addSublayer:gradientLayer];
[CATransaction commit];
}
}
There's another issue: UIKit is not thread safe. You can't call bounds on a UILabel on a background thread.
Check anywhere in your code there is an uncommitted CATransaction. You will need to perform this operation following way;
[CATransaction begin];
[CATransaction setDisableActions:YES];
[commonClassObject orangeGradient:lblFill];
[CATransaction commit];
[EDIT]
Basically this problem happens when NSOperation completes before CoreAnimation is done performing. There must be some relation between these two class in somewhere in your code you need to figure that out.
I'll just add that I had the same problem with initWithFrame from a background threads.
Seems that since iOS 7 and upwards, some calls to UIKit must be done under the main thread, and cannot be called from a background thread.
Also note that after several warnings like this, my app stopped responding completely. So better not ignore such warning.

Application crash on -[__NSCFSet bytes]: unrecognized selector sent to instance ""?

I developing an application that application well worked for me my XCode version is 4.2.1 and iOS version is 5.0 ARC enable code. My process is i showing a bunch of YouTube videos in tableview using web service. I cache those video Icon in my directory. When i sending that application for apple review... application going crash there, the crash report is following,
Exception Type: EXC_CRASH (SIGABRT)
Exception Codes: 0x00000000, 0x00000000
Crashed Thread: 2
Application Specific Information:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFSet bytes]: unrecognized selector sent to instance 0x27b300'
*** First throw call stack:
(0x3502e88f 0x36455259 0x35031a9b 0x35030915 0x34f8b650 0x34f7da4d 0x3587f2b9 0x3587ef31 0x3587ee9d 0x321102ab 0x321101c7 0xe36dd 0x31053c59 0x3105f7bb 0x36cd9dfb 0x36cd9cd0)
Thread 2 name: Dispatch queue: com.apple.root.default-priority
Thread 2 Crashed:
0 libsystem_kernel.dylib 0x34e2232c __pthread_kill + 8
1 libsystem_c.dylib 0x36d1e208 pthread_kill + 48
2 libsystem_c.dylib 0x36d17298 abort + 88
3 libc++abi.dylib 0x309e3f64 abort_message + 40
4 libc++abi.dylib 0x309e1346 _ZL17default_terminatev + 18
5 libobjc.A.dylib 0x36455350 _objc_terminate + 140
6 libc++abi.dylib 0x309e13be _ZL19safe_handler_callerPFvvE + 70
7 libc++abi.dylib 0x309e144a std::terminate() + 14
8 libc++abi.dylib 0x309e2798 __cxa_throw + 116
9 libobjc.A.dylib 0x36455290 objc_exception_throw + 88
10 CoreFoundation 0x35031a94 -[NSObject doesNotRecognizeSelector:] + 168
11 CoreFoundation 0x3503090e ___forwarding___ + 294
12 CoreFoundation 0x34f8b648 _CF_forwarding_prep_0 + 40
13 CoreFoundation 0x34f7da46 CFDataGetBytePtr + 90
14 ImageIO 0x3587f2b2 CGImageReadGetBytePointer + 30
15 ImageIO 0x3587ef2a _CGImageSourceBindToPlugin + 86
16 ImageIO 0x3587ee96 CGImageSourceGetCount + 50
17 UIKit 0x321102a4 _UIImageRefFromData + 124
18 UIKit 0x321101c0 -[UIImage initWithData:] + 52
19 MyAppName 0x000e36d6 __37-[JMImageCache imageForURL:delegate:]_block_invoke_0 + 142
20 libdispatch.dylib 0x31053c52 _dispatch_call_block_and_release + 6
21 libdispatch.dylib 0x3105f7b4 _dispatch_worker_thread2 + 256
22 libsystem_c.dylib 0x36cd9df4 _pthread_wqthread + 288
23 libsystem_c.dylib 0x36cd9cc8 start_wqthread + 0
Now my coding part in application ,
On Table view data source method
if(![[JMImageCache sharedCache] isImageExistInCache: cell.videoThumbnailImageLink]){
[cell.activityIndicator startAnimating];
cell.videoThumbnail.image = [[JMImageCache sharedCache] imageForURL:cell.videoThumbnailImageLink delegate:cell];
}
else{
[cell.activityIndicator stopAnimating];
[cell.activityIndicator removeFromSuperview];
cell.videoThumbnail.image = [[JMImageCache sharedCache] imageFromDiskForURL:cell.videoThumbnailImageLink];
}
And at custom cell of that tableview,
#pragma mark - JMImageCacheDelegate Methods
- (void) cache:(JMImageCache *)c didDownloadImage:(UIImage *)i forURL:(NSString *)url {
if([url isEqualToString:self.videoThumbnailImageLink]) {
self.imageView.image = i;
[self setNeedsLayout];
}
[self.activityIndicator stopAnimating];
[self.activityIndicator removeFromSuperview];
}
In JMImageCache library code block,
- (UIImage *) imageForURL:(NSString *)url delegate:(id<JMImageCacheDelegate>)d {
if(!url) {
return nil;
}
id returner = [super objectForKey:url];
if(returner) {
return returner;
} else {
UIImage *i = [self imageFromDiskForURL:url];
if(i) {
[self setImage:i forURL:url];
return i;
}
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
__unsafe_unretained NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:url]];
UIImage *i = [[UIImage alloc] initWithData:data];
__unsafe_unretained NSString* cachePath = cachePathForURL(url);
NSInvocation* writeInvocation = [NSInvocation invocationWithMethodSignature:[self methodSignatureForSelector:#selector(writeData:toPath:)]];
[writeInvocation setTarget:self];
[writeInvocation setSelector:#selector(writeData:toPath:)];
[writeInvocation setArgument:&data atIndex:2];
[writeInvocation setArgument:&cachePath atIndex:3];
[self performDiskWriteOperation:writeInvocation];
[self setImage:i forURL:url];
dispatch_async(dispatch_get_main_queue(), ^{
if(d) {
if([d respondsToSelector:#selector(cache:didDownloadImage:forURL:)]) {
[d cache:self didDownloadImage:i forURL:url];
}
}
});
});
return nil;
}
}
Thanks.
You have a memory problem. The hint is, that the variable which points to 0x27b300 in memory now contains a __NSCFSet (otherwise known as an NSSet). Make sure you're not using a variable which hasn't been nil'd out or is weak when it should be strong.
Your data is being released and deallocated before it's used, so when CoreGraphics calls -bytes on it, it's no longer there (and in this case, an NSSet has been allocated in its place).
Presumably this is because you declared it as __unsafe_unretained.

Cocos2D crashing on iPhone immediately but not in simulator

I'm trying to test a Cocos2D app on an iPhone, and get this crash that I copied from the console:
cocos2d: CCSpriteFrameCache: Trying to use file 'heroTestSheet.png' as texture
cocos2d: CCTexture2D. Can't create Texture. UIImage is nil
cocos2d: Couldn't add image:heroTestSheet.png in CCTextureCache
cocos2d: CCSpriteFrameCache: Couldn't load texture
cocos2d: CCTexture2D. Can't create Texture. UIImage is nil
cocos2d: Couldn't add image:heroTestSheet.png in CCTextureCache
cocos2d: CCSpriteFrameCache: Frame 'heroFrame1.png' not found
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSMutableArray insertObject:atIndex:]: attempt to insert nil object at 0'
*** Call stack at first throw:
(
0 CoreFoundation 0x3759dc7b __exceptionPreprocess + 114
1 libobjc.A.dylib 0x32d9bee8 objc_exception_throw + 40
2 CoreFoundation 0x3752a951 -[__NSArrayM insertObject:atIndex:] + 136
3 CoreFoundation 0x3752a8bf -[__NSArrayM addObject:] + 34
4 cocosTests 0x0000ce28 -[HeroClass init] + 1544
5 cocosTests 0x0000304c -[DebugZoneLayer init] + 860
6 cocosTests 0x00074e04 +[CCNode node] + 76
7 cocosTests 0x0000c4e4 -[DebugZoneScene init] + 244
8 cocosTests 0x00074e04 +[CCNode node] + 76
9 cocosTests 0x0000c390 +[DebugZoneScene scene] + 100
10 cocosTests 0x00002540 -[cocosTestsAppDelegate applicationDidFinishLaunching:] + 1028
11 UIKit 0x3592502c -[UIApplication _callInitializationDelegatesForURL:payload:suspended:] + 1200
12 UIKit 0x3591ea78 -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 396
13 UIKit 0x358d82e4 -[UIApplication handleEvent:withNewEvent:] + 1476
14 UIKit 0x358d7b1c -[UIApplication sendEvent:] + 68
15 UIKit 0x358d73b4 _UIApplicationHandleEvent + 6824
16 GraphicsServices 0x33e77c88 PurpleEventCallback + 1048
17 CoreFoundation 0x3752f5cb __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 28
18 CoreFoundation 0x3752f589 __CFRunLoopDoSource1 + 164
19 CoreFoundation 0x37521835 __CFRunLoopRun + 580
20 CoreFoundation 0x3752150b CFRunLoopRunSpecific + 226
21 CoreFoundation 0x37521419 CFRunLoopRunInMode + 60
22 UIKit 0x3591d554 -[UIApplication _run] + 548
23 UIKit 0x3591a558 UIApplicationMain + 972
24 cocosTests 0x000020c4 main + 100
25 cocosTests 0x0000205c start + 40
)
terminate called after throwing an instance of 'NSException'
Program received signal: “SIGABRT”.
warning: Unable to read symbols for /Developer/Platforms/iPhoneOS.platform/DeviceSupport/4.2.1 (8C148)/Symbols/Developer/usr/lib/libXcodeDebuggerSupport.dylib (file not found).
Running it in the simulator runs fine, but I still get this message in the console when it starts up "CCSpriteFrameCache: Trying to use file 'heroTestSheet.png' as texture"
I suspect that's where the problem starts. Maybe I'm just understanding how my code for my hero sprite subclass should be written. I thought heroTestSheet.png was the texture it was relying on to split up into other images referencing the plist.
Here's my init in my hero subclass:
-(id) init{
self = [super init];
if (!self) {
return nil;
}
_collisWidthFromCtr = 16;
_collisHeightFromCtr = 16;
_collisPushPointsNums = 5;
_travelRectCenterPoints = [[NSMutableArray alloc] init];
_collisPushPoints = [[NSMutableArray alloc] init];
[_collisPushPoints insertObject:[NSValue valueWithCGPoint:CGPointMake( _collisWidthFromCtr, _collisHeightFromCtr)] atIndex:0];
[_collisPushPoints insertObject:[NSValue valueWithCGPoint:CGPointMake( _collisWidthFromCtr, 0)] atIndex:1];
[_collisPushPoints insertObject:[NSValue valueWithCGPoint:CGPointMake( _collisWidthFromCtr,-_collisHeightFromCtr)] atIndex:2];
[_collisPushPoints insertObject:[NSValue valueWithCGPoint:CGPointMake( 0, _collisHeightFromCtr)] atIndex:3];
[_collisPushPoints insertObject:[NSValue valueWithCGPoint:CGPointMake(-_collisWidthFromCtr, _collisHeightFromCtr)] atIndex:4];
_rectCheckRes = 32;
_speed = 8;
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:#"heroTestSheet.plist"];
_heroSpriteSheet = [CCSpriteBatchNode batchNodeWithFile:#"heroTestSheet.png"];
//[self addChild:_heroSpriteSheet];
NSMutableArray *heroSpriteFlyAnimFrames = [NSMutableArray array];
for(int i = 1; i <= 2; ++i) {
[heroSpriteFlyAnimFrames addObject:
[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:
[NSString stringWithFormat:#"heroFrame%d.png", i]]];
}
CCAnimation *heroSpriteFlyAnim = [CCAnimation animationWithFrames:heroSpriteFlyAnimFrames delay:0.03f];
_heroSprite = [CCSprite spriteWithSpriteFrameName:#"heroFrame1.png"];
_heroSpriteFlyAction = [CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:heroSpriteFlyAnim restoreOriginalFrame:NO]];
[_heroSprite runAction:_heroSpriteFlyAction];
[_heroSpriteSheet addChild:_heroSprite];
return self;
}
Please excuse me if I'm being unclear, so let me know any other info I need to provide. Just posting this for now because I don't know where else to start to solve it and what other info I should provide, and it might be obvious to some other cocos devs whats wrong from what I posted. Thanks
The simulator can be more forgiving than the device. Double check that your file name really is "heroTestSheet.png" and not "HeroTestSheet.png" for example. It is case sensitive on your device.
Figured it out.. it was just the heroTestSheet.png it was trying to use was somehow invalid. I exported in an older photoshop, but I'm not really positive what settings it had. I exported it again from a newer photoshop without any color mange settings, and now it works fine!
I think you image was not added on the project.
You must copy your binary to the resource.
select your target and then select the build phases then copy bundle resource
add you image on that content.
I think it will work.

My application doesn't open after trying to implement iAds!

I have been trying to implement iAds into my application, but this is what happens:
1.Tap on app
2.Loading Screen Displayed for a few seconds
3.App Crashes
This is whats returned:
2010-11-06 20:19:11.043 Vampire Quiz
Final[99722:207] Unknown class
AdViewController in Interface Builder
file. 2010-11-06 20:19:11.066 Vampire
Quiz Final[99722:207]
-[Vampire_Quiz_FinalViewController setBannerIsVisible:]: unrecognized
selector sent to instance 0x761c710
2010-11-06 20:19:11.409 Vampire Quiz
Final[99722:207] * Terminating app
due to uncaught exception
'NSInvalidArgumentException',
reason:
'-[Vampire_Quiz_FinalViewController
setBannerIsVisible:]: unrecognized
selector sent to instance 0x761c710'
* Call stack at first throw: ( 0 CoreFoundation
0x02a88b99 exceptionPreprocess + 185
1 libobjc.A.dylib
0x02bd840e objc_exception_throw + 47
2 CoreFoundation
0x02a8a6ab -[NSObject(NSObject)
doesNotRecognizeSelector:] + 187 3
CoreFoundation
0x029fa2b6 __forwarding + 966 4
CoreFoundation
0x029f9e72 _CF_forwarding_prep_0 + 50
5 Vampire Quiz Final
0x000027a2
-[Vampire_Quiz_FinalViewController viewDidLoad] + 601 6 UIKit
0x003715ca -[UIViewController view] +
179 7 Vampire Quiz Final
0x000021b1
-[Vampire_Quiz_FinalAppDelegate application:didFinishLaunchingWithOptions:]
+ 74 8 UIKit 0x002c7f27 -[UIApplication
_callInitializationDelegatesForURL:payload:suspended:]
+ 1163 9 UIKit 0x002ca3b0 -[UIApplication
_runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:]
+ 346 10 UIKit 0x002d43ec -[UIApplication
handleEvent:withNewEvent:] + 1958 11
UIKit
0x002ccb3c -[UIApplication sendEvent:]
+ 71 12 UIKit 0x002d19bf _UIApplicationHandleEvent +
7672 13 GraphicsServices
0x03368822 PurpleEventCallback + 1550
14 CoreFoundation
0x02a69ff4
CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION
+ 52 15 CoreFoundation 0x029ca807 __CFRunLoopDoSource1 + 215
16 CoreFoundation
0x029c7a93 __CFRunLoopRun + 979 17
CoreFoundation
0x029c7350 CFRunLoopRunSpecific + 208
18 CoreFoundation
0x029c7271 CFRunLoopRunInMode + 97 19
UIKit
0x002c9c6d -[UIApplication _run] + 625
20 UIKit
0x002d5af2 UIApplicationMain + 1160
21 Vampire Quiz Final
0x00002144 main + 102 22 Vampire
Quiz Final 0x000020d5
start + 53 ) terminate called after
throwing an instance of 'NSException'
sharedlibrary apply-load-rules all
(gdb)
P.S. I am new to development on the iPhone
Thanks
This is my code :
#implementation Vampire_Quiz_FinalViewController
- (IBAction)V;
{
Vork *V = [[Vork alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:V animated:NO];
}
- (IBAction)A;
{
About *A = [[About alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:A animated:NO];
}
- (IBAction)I;
{
Instructions *I = [[Instructions alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:I animated:NO];
}
- (void)dealloc {
[super dealloc];
}
- (void)viewDidLoad {
adView = [[ADBannerView alloc] initWithFrame:CGRectZero];
adView.frame = CGRectOffset(adView.frame, 0, -50);
adView.requiredContentSizeIdentifiers = [NSSet setWithObject:ADBannerContentSizeIdentifier320x50];
adView.currentContentSizeIdentifier = ADBannerContentSizeIdentifier320x50;
[self.view addSubview:adView];
adView.delegate=self;
self.bannerIsVisible=NO;
[super viewDidLoad];
}
- (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
if (!self.bannerIsVisible)
{
[UIView beginAnimations:#"animateAdBannerOn" context:NULL];
// banner is invisible now and moved out of the screen on 50 px
banner.frame = CGRectOffset(banner.frame, 0, 50);
[UIView commitAnimations];
self.bannerIsVisible = YES;
}
}
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
if (self.bannerIsVisible)
{
[UIView beginAnimations:#"animateAdBannerOff" context:NULL];
// banner is visible and we move it out of the screen, due to connection issue
banner.frame = CGRectOffset(banner.frame, 0, -50);
[UIView commitAnimations];
self.bannerIsVisible = NO;
}
}
#end
How can I fix it???
you are using self.bannerIsVisible but I can't see neither a synthesize for this nor setters and getters. Did you made a property with bannerIsVisible in your .h file?
To resolve this crash you should define the property in you header and add a #synthesize statement in your implementation.
Maybe you should start with something more basic to get to know the fundamental things like properties, synthesizers, compiler warnings (there should be one), debugging, and so on.
I don't want to be rude, but you won't learn much by using copied code you don't understand.
unrecognized selector sent to instance: this means the method for that class is not found. Check the class implementation.

How to ensure that UIImage is never released?

I grabbed the crash log from the iPhone:
Exception Type: EXC_BAD_ACCESS (SIGBUS)
Exception Codes: KERN_PROTECTION_FAILURE at 0x0000000c
Crashed Thread: 0
Thread 0 Crashed:
0 libobjc.A.dylib 0x30011940 objc_msgSend + 20
1 CoreFoundation 0x30235f1e CFRelease + 98
2 UIKit 0x308f4974 -[UIImage dealloc] + 36
3 CoreFoundation 0x30236b72 -[NSObject release] + 28
4 UIKit 0x30a00298 FlushNamedImage + 64
5 CoreFoundation 0x30250a20 CFDictionaryApplyFunction + 124
6 UIKit 0x30a0019c _UISharedImageFlushAll + 196
7 UIKit 0x30a00730 +[UIImage(UIImageInternal) _flushCacheOnMemoryWarning:] + 8
8 Foundation 0x3054dc7a _nsnote_callback + 178
9 CoreFoundation 0x3024ea52 _CFXNotificationPostNotification + 298
10 Foundation 0x3054b854 -[NSNotificationCenter postNotificationName:object:userInfo:] + 64
11 Foundation 0x3054dbba -[NSNotificationCenter postNotificationName:object:] + 14
12 UIKit 0x30a00708 -[UIApplication _performMemoryWarning] + 60
13 UIKit 0x30a006a0 -[UIApplication _receivedMemoryNotification] + 128
14 UIKit 0x30a005d0 _memoryStatusChanged + 56
15 CoreFoundation 0x30217410 __CFNotificationCenterDarwinCallBack + 20
16 CoreFoundation 0x3020d0aa __CFMachPortPerform + 72
17 CoreFoundation 0x30254a70 CFRunLoopRunSpecific + 2296
18 CoreFoundation 0x30254164 CFRunLoopRunInMode + 44
19 GraphicsServices 0x3204529c GSEventRunModal + 188
20 UIKit 0x308f0374 -[UIApplication _run] + 552
21 UIKit 0x308eea8c UIApplicationMain + 960
...
...
From my previous question, Can somebody give me a hand about this stacktrace in iPhone app?, I have changed my codes mainly around UIImage part. I now use [[UIImage alloc] initWithContentsOfFile ... ]. No more [UIImage imageNamed: ... ] or the like. The portion is below.
//this is a method of a subclass of UIImageView.
- (void) reviewImage: (bool) review{
NSString* st;
if (review){
NSString* origin = [NSString stringWithString: [[ReviewCardManager getInstance] getCardImageString:chosenIndex]];
NSString* stt = [origin substringToIndex: [origin length]-4];
st = [[NSString alloc] initWithString: stt];
if (myImageFlipped == nil)
myImageFlipped = [[UIImage alloc] initWithContentsOfFile: [[NSBundle mainBundle] pathForResource:st ofType:#"png"]];
[self setImage:myImageFlipped];
if (notRotated){
self.transform = CGAffineTransformRotate(self.transform, [MyMath radf:rotate]);
notRotated = false;
}
}else{
st = [[NSString alloc] initWithFormat:#"sc%d", chosenNumber];
if (myImage == nil)
myImage = [[UIImage alloc] initWithContentsOfFile: [[NSBundle mainBundle] pathForResource:st ofType:#"png"]];
[self setImage:myImage];
if (notRotated){
self.transform = CGAffineTransformRotate(self.transform, [MyMath radf:rotate]);
notRotated = false;
}
}
[st release];
}
I also have the UIImage already retained in the property.
#property (nonatomic, retain) UIImage* myImage, *myImageFlipped;
Memory Leaks have also been taken cared of. These variables are release in dealloc method.
I thought that I have successfully killed the bug, but it seems that I still have a rare occuring bug problem.
Based on the crash log, my application yells out "performMemoryWarning". I am just "alloc"-ing 13 .png images with the size 156 x 272. I'm confused. Those images shouldn't take that much memory to the point that it exceeds iPhone's RAM. Or is there something I am overlooking? Please advise.
To help you with memory issues and UIImages, you might want to use the imageNamed convience method of UIImage, from the docs:
This method looks in the system caches for an image object with the specified name and returns that object if it exists. If a matching image object is not already in the cache, this method loads the image data from the specified file, caches it, and then returns the resulting object.
Alternatively, you might want to go this route if you still run into memory issues after switching to UIImage imageNamed, because there are some downsides to using the convinience method.
The problem is solved. I forgot to change UIImage at one place. Now, all UIImages are truly "alloc", no more autorelease.
FYI, if you are using [UIImage imageNamed: ... ], use "Simulate Memory Warning" on iPhone Simulator to see whether you are having a problem with it when the real device is low on memory.