10

So I was attempting to test a PhoneGap application I've been working on, and had some issues with the test on my iPad. I have the following as the main method for this application:

//
//  main.m
//  elog
//
//  Created by Ben Potter on 9/08/11.
//  Copyright Arden Anglican School 2011. All rights reserved.
//

#import <UIKit/UIKit.h>

int main(int argc, char *argv[]) {

    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    int retVal = UIApplicationMain(argc, argv, nil, @"AppDelegate");
    [pool release];
    return retVal;
}

It all works fine until you close the app completely and then reopen it, upon which it freezes. And I have no idea why.

I am running ios5 on my ipad with XCode 4.2

Finally, these are the errors which come up, thanks for the help!

Main method errors Direct link here

Perception
  • 79,279
  • 19
  • 185
  • 195
Ben Potter
  • 875
  • 5
  • 20
  • 34
  • XCode 4.2 and iOS 5 are still under NDA. Your error messages are speaking volumes btw. See here - http://stackoverflow.com/questions/6655842/nsautoreleasepool-is-unavailable. – Perception Aug 10 '11 at 14:34

5 Answers5

19

Your application is using Automatic Reference Counting (which is new) and Phonegap doesn't support it yet. Go to your project's build settings and turn Automatic Reference Counting to off.

dtuckernet
  • 7,817
  • 5
  • 39
  • 54
9

This is how I solved the "NSAutoReleasePool" error in XCode 4.3 and with PhoneGap 1.5.

  1. Go to "YourApplicationName" in the Project Navigator.

  2. Select "YourApplicationName" under Project.

  3. Go to Build Settings.

  4. Make sure to toggle "All" and "Combined".

  5. Find the section "Apple LLVM compiler 3.1 - Language".

  6. Scroll down and you will find "Objective-C Automatic Reference Counting".

  7. Change it from Yes to No.

Try to build your project again and you should be fine!

Nicklas Savonen
  • 159
  • 3
  • 10
  • Stunning! I always changed from Apple LLVM to LLVM GCC, but this only partly rectified the problem. Your advice solved it once and for all, and nou I can uses Apple LLVM. Thanks. – Jack May 22 '12 at 07:30
1

So, just to clarify for the visually-inclined, it took me a few minutes to find the correct option since it's only mentioned in the comments of another answer. I had to find the CLANG_ENABLE_OBJC_ARC build flag and switch it to NO. You'll find it under the Build settings in the User-Defined section (very bottom for me):

enter image description here

I was also able to get past the runtime error by going into main.m and commenting out the NSAutoreleasePool setup code like so:

//NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int retVal = UIApplicationMain(argc, argv, nil, @"AppDelegate");
//[pool release];
return retVal;

However, I'm not sure what other effects that might have down the line. It seems that at present, while working with PhoneGap, it's probably best to stick with manual ref counting until PhoneGap properly supports ARC.

And just for Google, the error that led me here was "NSAutoreleasePool is unavailable" as I don't see that as text in the original post.

Brian Moeskau
  • 20,103
  • 8
  • 71
  • 73
0

Prior to cordova 2.1.0 ARC is not supported, you are not supposed tick the box below (when you are still creating your project): enter image description here

However, this is the exact code you need:

#import <UIKit/UIKit.h>

int main(int argc, char *argv[]) {

@autoreleasepool {
    int retVal = UIApplicationMain(argc, argv, nil, @"AppDelegate");
    return retVal;
    }
}

If you upgrade to cordova 2.1.0 in Xcode 4.5.1 you can go to: Edit Refactor The choose between Objective ARC and Modern Objective-C syntax. enter image description here

0

If you don't want to disable ARC, then the following will fix the errors in Xcode 4.2.

AppDelegate.m

  //    self.window = [[[UIWindow alloc] initWithFrame:screenBounds]autorelease];
      self.window = [[UIWindow alloc] initWithFrame:screenBounds];

  //    self.viewController = [[[MainViewController alloc] init] autorelease];
      self.viewController = [[MainViewController alloc] init];

(void) dealloc
{
  //    [super dealloc];
}

main.m

int main(int argc, char *argv[]) {
//   NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
//   int retVal = UIApplicationMain(argc, argv, nil, @"AppDelegate");
//   [pool release];
//   return retVal;
  @autoreleasepool {
    NSLog (@"Programming is fun!");
  }
  return 0;    
}

Disclaimer:

Not sure how this affects the rest of the PhoneGap lib an plugins though. However, after implementing these changes, the template PhoneGap project runs on the simulator, but terminates instantly. This was just a starting point.

Better Alternative:

I suggest you disable Automatic Reference Counting (ARC) under Build Settings until PhoneGap supports it. This thread on on PhoneGap forum hints that ARC may be supported in PhoneGap 1.6.

Jack
  • 16,506
  • 19
  • 100
  • 167