0

Once I run application on iPad I see crash of application within the initWithImage function. It does not happen on simulator.

@implementation CCTexture2D (Image)

- (id) initWithImage:(UIImage *)uiImage

... 

    // Create the bitmap graphics context

    switch(pixelFormat) {          
        case kCCTexture2DPixelFormat_RGBA8888:
        case kCCTexture2DPixelFormat_RGBA4444:
        case kCCTexture2DPixelFormat_RGB5A1:
            colorSpace = CGColorSpaceCreateDeviceRGB();
            data = malloc(POTHigh * POTWide * 4);
            info = hasAlpha ? kCGImageAlphaPremultipliedLast : kCGImageAlphaNoneSkipLast; 
//          info = kCGImageAlphaPremultipliedLast;  // issue #886. This patch breaks BMP images.
            context = CGBitmapContextCreate(data, POTWide, POTHigh, 8, 4 * POTWide, colorSpace, info | kCGBitmapByteOrder32Big);                
            CGColorSpaceRelease(colorSpace);
            break;

        case kCCTexture2DPixelFormat_RGB565:
            colorSpace = CGColorSpaceCreateDeviceRGB();
            data = malloc(POTHigh * POTWide * 4);
            info = kCGImageAlphaNoneSkipLast;
            context = CGBitmapContextCreate(data, POTWide, POTHigh, 8, 4 * POTWide, colorSpace, info | kCGBitmapByteOrder32Big);
            CGColorSpaceRelease(colorSpace);
            break;
        case kCCTexture2DPixelFormat_A8:
            data = malloc(POTHigh * POTWide);
            info = kCGImageAlphaOnly; 
            context = CGBitmapContextCreate(data, POTWide, POTHigh, 8, POTWide, NULL, info);
            break;                    
        default:
            [NSException raise:NSInternalInconsistencyException format:@"Invalid pixel format"];
    }


    CGContextClearRect(context, CGRectMake(0, 0, POTWide, POTHigh));
    CGContextTranslateCTM(context, 0, POTHigh - imageSize.height);

===================
crash here
--->    CGContextDrawImage(context, CGRectMake(0, 0, CGImageGetWidth(CGImage), CGImageGetHeight(CGImage)), CGImage);

This file is loaded with LevelHelper (png file was created with SpriteHelper). It works fine once loaded for the first time. It breakes on nect attempt after the memory warning is handled (and the texture cache is cleaned). Could you please help with the next steps of investigations?

Marcin
  • 1

2 Answers2

0

Crashing happens when you have a null pointer somewhere in your code, in your case, it's probably because the uiImage was not added or was deleted from your app's bundle.

It's a very common thing in iOS development and this type of bug is fairly difficult to debug, but a good starting point is to set your nszombieenabled to true on your debug phase. Check this thread to see how:

How do I set up NSZombieEnabled in Xcode 4?

As for your app crashing on the iPad but not on the simulator, this is quite common as well. Usually selecting Product/Clean will fix the problem and the app will start crashing on the simulator as well as on the iPad.

Sometimes it also helps setting the simulator back to 'factory settings' before hitting 'Run'. To do that select 'Reset Content and Settings' from the top menu on the iOS simulator. And finally, sometimes completely deleting the app from your iPad before compiling it again also helps to get them to behave the same.

Community
  • 1
  • 1
aeldron
  • 1,575
  • 1
  • 13
  • 16
  • Thank you! You pointed me a valid way. In fact I had the file in the boundle and what is more interesting the pointer that was causing problems wat not null. But... I enabled memory guard and reproduce the issue on simulator. It sounds like I did not got the request for another 16MB fulfilled. On iPad I got the pointer for context (looks good) but the draw method caused an exception as went outside the boundary. SO the question is: is it possible to get the non-null pointer from malloc method that is pointing to the memory that is not fully available? – Marcin Feb 01 '12 at 23:13
0

OK... finally got that:)

I run out of memory cause I was not releasing the CCLayer properly. In simple in clean section I had to unchedule ticker and touch dispatcher so that release could deallocate rest of the stuff.

Still got a problem with understanding how come that on the iPad malloc would return not-null value for the memory and in fact has it corrupted. But this is a question for another thread:)

Thank you!

Marcin
  • 1