How to ensure that UIImage is never released? - iphone

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.

Related

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.

iphone table view scroll aborts - unrecognized selector

I know there have been a ton of questions about the same thing, but so far I haven't been able to apply any solutions to my problem. And I still haven't figured out how to use Instruments.
I'm taking a basic tutorial for an iPhone app and just trying to tweak it slightly (I am new to Objective C). I want it to read from a plist with an array of dicts instead of an array of strings. The table initially displays the data correctly. However whenever I scroll the table up (and off the screen), I am getting Unrecognized Selector exceptions. Just populating employees with NSStrings works fine. I am lost.
Relevant portions of the ViewController:
#interface RootViewController : UITableViewController {
NSMutableArray *employees_;
}
#property (nonatomic, retain) NSMutableArray *employees;
#end
and
#implementation RootViewController
#synthesize employees=employees_;
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *path = [[NSBundle mainBundle] pathForResource:#"Employees" ofType:#"plist"];
NSMutableArray *empArray = [[NSMutableArray alloc] initWithContentsOfFile:path];
employees_ = [empArray valueForKey:#"name"];
[empArray release];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell.
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.text = [self.employees objectAtIndex:indexPath.row];//this is where it errors
return cell;
}
- (void)dealloc
{
[employees_ release];
[super dealloc];
}
#end
and plist:
array
dict
key name /key
string Employee One /string
key id /key
string T1234 /string
/dict
dict
key name /key
string Employee Two /string
key id /key
string T5678 /string
/dict
/array
Error that I received:
2011-10-18 20:02:44.313 MyApp[65148:bc03] -[NSCFString objectAtIndex:]: unrecognized selector sent to instance 0x689a050
2011-10-18 20:02:44.316 MyApp[65148:bc03] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCFString objectAtIndex:]: unrecognized selector sent to instance 0x689a050'
*** Call stack at first throw:
(
0 CoreFoundation 0x00dc25a9 __exceptionPreprocess + 185
1 libobjc.A.dylib 0x00f16313 objc_exception_throw + 44
2 CoreFoundation 0x00dc40bb -[NSObject(NSObject) doesNotRecognizeSelector:] + 187
3 CoreFoundation 0x00d33966 ___forwarding___ + 966
4 CoreFoundation 0x00d33522 _CF_forwarding_prep_0 + 50
5 MyApp 0x00002a96 -[RootViewController tableView:cellForRowAtIndexPath:] + 326
6 UIKit 0x00089b98 -[UITableView(UITableViewInternal) _createPreparedCellForGlobalRow:withIndexPath:] + 634
7 UIKit 0x0007f4cc -[UITableView(UITableViewInternal) _createPreparedCellForGlobalRow:] + 75
8 UIKit 0x000948cc -[UITableView(_UITableViewPrivate) _updateVisibleCellsNow:] + 1561
9 UIKit 0x0008c90c -[UITableView layoutSubviews] + 242
10 QuartzCore 0x016aca5a -[CALayer layoutSublayers] + 181
11 QuartzCore 0x016aeddc CALayerLayoutIfNeeded + 220
12 QuartzCore 0x016540b4 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 310
13 QuartzCore 0x01655294 _ZN2CA11Transaction6commitEv + 292
14 QuartzCore 0x0165546d _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 99
15 CoreFoundation 0x00da389b __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 27
16 CoreFoundation 0x00d386e7 __CFRunLoopDoObservers + 295
17 CoreFoundation 0x00d011d7 __CFRunLoopRun + 1575
18 CoreFoundation 0x00d00840 CFRunLoopRunSpecific + 208
19 CoreFoundation 0x00d00761 CFRunLoopRunInMode + 97
20 GraphicsServices 0x00ffa1c4 GSEventRunModal + 217
21 GraphicsServices 0x00ffa289 GSEventRun + 115
22 UIKit 0x00022c93 UIApplicationMain + 1160
23 MyApp 0x00002249 main + 121
24 MyApp 0x000021c5 start + 53
)
terminate called throwing an exceptionCurrent language: auto; currently objective-c
(gdb)
There are two potential problems:
You need to make sure the call to employees_ = [empArray valueForKey:#"name"] is actually returning a NSArray
Once one is ruled out, and assuming you are not using ARC, your employees_ ivar is getting released before the table view gets a chance to configure itself. Try
employees_ = [[empArray valueForKey:#"name"] retain];
And then release employees_ in your viewDidUnload & dealloc methods.
Hard to tell it from the stack as it does say your ivar is a NSCFString but it could be just because it is referencing an invalid/garbage memory address. Based on your plist description though, the likely cause on on point #1.
-[NSCFString objectAtIndex:]: unrecognized selector
This means you tried to send the selector (i.e. call the method) objectAtIndex: on an instance of class NSCFString (which is the same as NSString). ("Next Step / Core Foundation String")
You can't do that.
Since you only are calling objectAtIndex: in one place in your above code, it's easy to see where the problem is. (Besides which in your debugger you should be able to see what line it happens on.) You're calling [self.employees objectAtIndex:...]. Obviously you expect self.employees to be an array, but it is a string instead. I don't really know about plist processing, so figuring out why employees got a string and how to make it get an array instead is up to you.

iPhone: SIGSEGV occurs seemingly randomly (ViewDidLoad, setFont, etc.) on device, never in simulator

First, thanks in advance for taking the time to read through this. I have been searching for an answer to this issue for a few days but I cannot seem to find what I am looking for. I know that SIGSEGV errors (or EXEC_BAD_ACCESS faults) are usually caused by memory management issues but after hours of looking at the code, I can't seem to find an error. The static analyzer hasn't found anything either.
As the title says, this error only occurs on the device (never in the simulator) and doesn't happen every time the given view loads. Obviously this means that I can't use NSZombie to debug what is going wrong.
Let's start with the crash dump log:
0 myApp 0x00036659 +[TFCrashHandler backtrace] + 428
1 myApp 0x00036a6f TFSignalHandler + 54
2 libsystem_c.dylib 0x32f48539 _sigtramp + 48
3 UIKit 0x365701df -[UIView(Rendering) setNeedsDisplay] + 86
4 UIKit 0x36598269 -[UILabel setFont:] + 232
5 myApp 0x000190eb +[BruUISettings applyLabelDetailStyle:] (BruUISettings.m:130)
6 myApp 0x0001aa57 -[ActiveChallengeViewController viewDidLoad] (ActiveChallengeViewController.m:115)
7 UIKit 0x365a57ff -[UIViewController view] + 166
8 UIKit 0x365b1c39 -[UIViewController contentScrollView] + 24
9 UIKit 0x365b1aa9 -[UINavigationController _computeAndApplyScrollContentInsetDeltaForViewController:] + 36
10 UIKit 0x365b198f -[UINavigationController _layoutViewController:] + 34
11 UIKit 0x365b115b -[UINavigationController _startTransition:fromViewController:toViewController:] + 318
12 UIKit 0x365b0f53 -[UINavigationController _startDeferredTransitionIfNeeded] + 250
13 UIKit 0x365a5673 -[UINavigationController pushViewController:transition:forceImmediate:] + 806
14 UIKit 0x365a5349 -[UINavigationController pushViewController:animated:] + 36
15 myApp 0x00015dc7 -[ChallengesHomeViewController tableView:didSelectRowAtIndexPath:] (ChallengesHomeViewController.m:325)
16 UIKit 0x3661f565 -[UITableView _selectRowAtIndexPath:animated:scrollPosition:notifyDelegate:] + 944
17 UIKit 0x36697ce7 -[UITableView _userSelectRowAtPendingSelectionIndexPath:] + 158
18 Foundation 0x31f7a943 __NSFireDelayedPerform + 414
19 CoreFoundation 0x34ab1a63 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 14
20 CoreFoundation 0x34ab16c9 __CFRunLoopDoTimer + 364
21 CoreFoundation 0x34ab029f __CFRunLoopRun + 1206
22 CoreFoundation 0x34a334dd CFRunLoopRunSpecific + 300
23 CoreFoundation 0x34a333a5 CFRunLoopRunInMode + 104
24 GraphicsServices 0x351a7fed GSEventRunModal + 156
25 UIKit 0x3659a743 UIApplicationMain + 1090
26 myApp 0x000026c5 main (main.m:13)
27 myApp 0x0000265c start + 52
Note, I am using TestFlight from testflightapp.com and it is there exception handler at the top of the stack.
Here is the code that is creating and pushing the view controller in question:
CDAcceptedChallenge *challengeAtIndex = [self.activeChallenges objectAtIndex:[indexPath row]];
ActiveChallengeViewController *view = [[ActiveChallengeViewController alloc] initWithNibName:#"ActiveChallengeViewController"
bundle:nil
activeChallenge:challengeAtIndex];
view.leaveChallengeTarget = self;
[self.navigationController pushViewController:view animated:YES];
[view release];
This code is running inside of a didSelectRowAtIndex method, if that matters at all. This view controller has a bunch of member variables and IBOutlets which reference objects in the nib but here are the ones that correlate to the object mentioned in this error:
in .h:
UILabel *helpLabel;
...
#property (nonatomic, retain) IBOutlet UILabel *helpLabel;
in .m:
#synthesize helpLabel;
I am positive that helpLabel is bound to the correct object in the nib because it does get the correct styling when this doesn't blow up. Next, here is the init method for this view controller:
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self)
{
self.activeChallenge = inActiveChallenge;
}
return self;
Here is the viewDidLoad method where we will sometimes see the crash (bolded). Note, there is a lot going on here but nothing touches the self.helpLabel which is what eventually triggers the crash.
[super viewDidLoad];
[self.navigationController setNavigationBarHidden:FALSE];
self.navigationItem.title = #"Details";
// overlay disclosure creation
self.overlayDisclosure = [CustomColoredDisclosureIndicator accessoryWithColor:self.challengeNameLabel.textColor];
self.overlayDisclosure.highlightedColor = [UIColor whiteColor];
[self.overlayDisclosure addTarget:self action:#selector(overlayViewButtonClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.overlayDisclosure];
[[BruNotificationManager instance] registerForTabUpdated:[[[BruCallback alloc] initWithTarget:self
andCallback:#selector(tabUpdated:)] autorelease]];
[[BruNotificationManager instance] registerForActiveChallengesUpdated:[BruCallback buildFromObject:self
andCallback:#selector(challengesUpdatedLocally)]];
[[BruNotificationManager instance] registerForEarnedRewardsUpdated:[BruCallback buildFromObject:self
andCallback:#selector(rewardsUpdatedLocally:)]];
[super refreshEarnedRewardsForChallenge:self.activeChallenge.challenge.identifier];
[super refreshTabForChallenge:self.activeChallenge.challenge.identifier];
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self
action:#selector(addBeer)];
self.navigationItem.rightBarButtonItem = addButton;
[addButton release];
// Request that the object download its image
BruCallback *imageCallback = [BruCallback buildFromObject:self andCallback:#selector(imageRequestCompleted:withData:)];
self.challengeImageView.image = [self.activeChallenge.challenge retrieveImage:self.bruConnection withCallback:imageCallback];
// Create our help label
**[BruUISettings applyLabelDetailStyle:self.helpLabel];**
self.helpLabel.textColor = AccentSecondaryColor;
self.overlayView.alpha = [BruUISettings standardOverlayViewAlpha];
[BruUISettings applyTableStyle:self.rewardsTable];
[BruUISettings applyScrollViewStyle:self.scrollView];
self.needToUpdateTab = YES;
self.needToUpdateRewards = YES;
self.needToUpdateChallenge = NO;
Here is a trimmed down version of the viewDidUnload method, though I don't see why it would matter in this case:
...
self.helpLabel = nil;
...
[super viewDidUnload];
Finally, my [BruUISettings applyLabelDetailStyle] is a method that applies a centralized style to my label. There are a bunch of places in my code that use this function so that I can change all of the styles for those labels by just changing the one function.
+ (void) applyLabelDetailStyle:(UILabel *)inLabel
{
inLabel.font = [UIFont fontWithName:#"Helvetica Neue" size:12.0];
inLabel.textColor = [UIColor darkGrayColor];
inLabel.backgroundColor = [UIColor clearColor];
}
Any thoughts as to what could be happening here would be greatly appreciated. Thanks again.
After a lot of debugging I found that this was actually due to a memory corruption being caused by the QSStrings library which I was using to generate base64 encoded strings. The crashes seemed to happen randomly but only after this library was invoked. When I switched to a different MD5 library, the issue went away. If you are using this library, I would suggest finding a new one to do base64 encoding. I moved to the one found here: http://www.cocoadev.com/index.pl?BaseSixtyFour
I think the problem happens because you have something wrong in your view that owns your label (view of your ActiveChallengeViewController).
When you set the font of your label, that invokes the setNeedsDisplay of your parent view and because something is wrong, setNeedsDisplay crashs.
Your line :
self.challengeImageView.image = [self.activeChallenge.challenge retrieveImage:self.bruConnection withCallback:imageCallback];
Is it a synchronous connection to retrieve the image ?
Maybe the problem happen here when sometimes the image isn't retrieved from your connection and so the image property of your image view is bad.
But i am not sure because setNeedsDisplay doesn't directly invoke the redraw of the view so i don't know if the debugger catches the error just the after setNeedsDisplay call.
Edit :
I think this answer is bad, if image is not ok, the crash must happen when the imageView image property retains the image but i let the answer in the case of it could help you to point to the real problem.

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.

Code data issue after upgrade to XCode + iOS 4.2

NSEntityDescription *entity = [NSEntityDescription entityForName:#"Thread" inManagedObjectContext:managedObjectContext];
That line doesn't seem to work anymore (I'm pretty sure its that line).
I can't seem to work out whats the problem. The application worked perfectly on Xcode with iOS 4.1 and now crashes with this error in the console:
2010-12-07 17:12:27.552 SMSApp[9222:207] +[ persistentStoreCoordinator]: unrecognized selector sent to class 0x5b14580
2010-12-07 17:12:27.553 SMSApp[9222:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+[ persistentStoreCoordinator]: unrecognized selector sent to class 0x5b14580'
*** Call stack at first throw:
(
0 CoreFoundation 0x01547be9 __exceptionPreprocess + 185
1 libobjc.A.dylib 0x0169c5c2 objc_exception_throw + 47
2 CoreFoundation 0x015497bb +[NSObject(NSObject) doesNotRecognizeSelector:] + 187
3 CoreFoundation 0x014b9366 ___forwarding___ + 966
4 CoreFoundation 0x014b8f22 _CF_forwarding_prep_0 + 50
5 CoreData 0x00e6f3ca +[NSEntityDescription entityForName:inManagedObjectContext:] + 42
6 SMSApp 0x000046b9 -[MessagesRootViewController reloadMessages:] + 146
7 SMSApp 0x00004a09 -[MessagesRootViewController viewDidLoad] + 85
8 UIKit 0x0052265e -[UIViewController view] + 179
9 UIKit 0x00520a57 -[UIViewController contentScrollView] + 42
10 UIKit 0x00531201 -[UINavigationController _computeAndApplyScrollContentInsetDeltaForViewController:] + 48
11 UIKit 0x0052f831 -[UINavigationController _layoutViewController:] + 43
12 UIKit 0x00530b4c -[UINavigationController _startTransition:fromViewController:toViewController:] + 524
13 UIKit 0x0052b606 -[UINavigationController _startDeferredTransitionIfNeeded] + 266
14 UIKit 0x00643e01 -[UILayoutContainerView layoutSubviews] + 226
15 QuartzCore 0x010b4451 -[CALayer layoutSublayers] + 181
16 QuartzCore 0x010b417c CALayerLayoutIfNeeded + 220
17 QuartzCore 0x010ad37c _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 310
18 QuartzCore 0x010ad0d0 _ZN2CA11Transaction6commitEv + 292
19 UIKit 0x0047719f -[UIApplication _reportAppLaunchFinished] + 39
20 UIKit 0x00477659 -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 690
21 UIKit 0x00481db2 -[UIApplication handleEvent:withNewEvent:] + 1533
22 UIKit 0x0047a202 -[UIApplication sendEvent:] + 71
23 UIKit 0x0047f732 _UIApplicationHandleEvent + 7576
24 GraphicsServices 0x01b2ca36 PurpleEventCallback + 1550
25 CoreFoundation 0x01529064 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 52
26 CoreFoundation 0x014896f7 __CFRunLoopDoSource1 + 215
27 CoreFoundation 0x01486983 __CFRunLoopRun + 979
28 CoreFoundation 0x01486240 CFRunLoopRunSpecific + 208
29 CoreFoundation 0x01486161 CFRunLoopRunInMode + 97
30 UIKit 0x00476fa8 -[UIApplication _run] + 636
31 UIKit 0x0048342e UIApplicationMain + 1160
32 SMSApp 0x000025c4 main + 102
33 SMSApp 0x00002555 start + 53
)
terminate called after throwing an instance of 'NSException'
Any idea where this error is coming from or what is causing it?
Just to let you know as well, upgrading to XCode with iOS 4.2 caused some of the files to be deleted from my project. I don't know why this was, whether it was because they were linked files and not actually in the folder I dunno.
Any ideas?
Thanks
// EDIT
This method is in the AppDelegate:
- (NSManagedObjectContext *)managedObjectContext {
if (managedObjectContext_ != nil) {
return managedObjectContext_;
}
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (coordinator != nil) {
managedObjectContext_ = [[NSManagedObjectContext alloc] init];
[managedObjectContext_ setPersistentStoreCoordinator:coordinator];
}
return managedObjectContext_;
}
// EDIT AGAIN
- (void)reloadMessages:(NSNotification *)notification {
NSLog(#"RECIEVED NEW MESSAGES TO MessagesRootViewController");
NSLog(#"%#",managedObjectContext);
// we need to get the threads from the database...
NSFetchRequest *theReq = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:#"Thread" inManagedObjectContext:self.managedObjectContext];
[theReq setEntity:entity];
threads = [NSArray arrayWithArray:[managedObjectContext executeFetchRequest:theReq error:nil]];
[self.tableView performSelectorOnMainThread:#selector(reloadData) withObject:nil waitUntilDone:NO];
if(notification != nil){ // if its been caused by a notification
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"New Message" message:#"A new message has been received" delegate:nil cancelButtonTitle:#"Thanks" otherButtonTitles:nil];
[alert performSelectorOnMainThread:#selector(show) withObject:nil waitUntilDone:NO];
[alert release];
}
}
// EDIT 3
If I put this code in the applicationDidFinishLoading of my app delegate it works fine!
NSFetchRequest *theReq = [[NSFetchRequest alloc] init];
NSEntityDescription *theEntity = [NSEntityDescription entityForName:#"Thread" inManagedObjectContext:self.managedObjectContext];
[theReq setEntity:theEntity];
NSArray *theThreads = [NSArray arrayWithArray:[self.managedObjectContext executeFetchRequest:theReq error:nil]];
After having a quick look at your code I think I found a couple of parts that needed attention. I'll try and break them down below:
SMSAppAppDelegate
1) Notice that in your interface file, you declare your CoreData variables with an underscore at the end of the variable name. Then you create properties for each of the ivar without the underscore. That is all fine as long as you #synthesize them making the correct assignment, which seems to be missing from your implementation:
#synthesize managedObjectContext=managedObjectContext_;
#synthesize managedObjectModel=managedObjectModel_;
#synthesize persistentStoreCoordinator=persistentStoreCoordinator_;
DownloadContacts
1) In this class you are calling the managedObjectContext directly through the variable. I wouldn't recommend that but this is not your actual problem. Notice that you release the managedObjectContext on dealloc even though you have no ownership of it!
2) What I would do in this case is declare a property for the context and always access it via the property. Also if you rename your instance variable to include an underscore at the end you are minimising your chances of accessing it directly (i.e. without using self.) and it will make it easier for you to find where in this class you are actually doing that (there are a couple of places from memory).
So in your interface file, rename managedObjectContext ivar and declare a property:
NSManagedObjectContext *managedObjectContext_;
...
#property (nonatomic, retain) NSManagedObjectContext managedObjectContext;
And don't forget to make the assignment on #sythesize and release on dealloc, since you are using a retain property:
#synthesize managedObjectContext=managedObjectContext_;
...
[managedObjectContext_ release];
Then if you try and compile you will get two or three errors of unrecognised instance variable which is where you have to go and change it to self.managedObjectContext
MessagesRootViewController
And finally I'd recommend you go through the same process in your MessagesRootViewController but this one should still work fine as is!
See how you go and let me know if any of the above is not clear!
Cheers,
Rog
I'm assuming you're doing something like:
[ClassName persistentStoreCoordinator]
Make sure ClassName is defined and has the persistentStoreCoordinator class method. Perhaps the file that did these things got deleted?
Matt
Your database might be corrupted, try resetting the iOS simulator
by clicking on iOS Simulator>Reset contents and settings.
Edit:
It looks like you're trying to call a selector that doesn't exist. Try figuring out which files were deleted, as it seems like you have a method that is called that can't be found at runtime. Did your core data classes get erased, maybe?