Note that there are some explanatory texts on larger screens.

plurals
  1. POiOS 5: Some references not being retained
    primarykey
    data
    text
    <p>I'm seeing some interesting behavior now that I'm using iOS 5 and Xcode 4.2. I have an singleton class that contains a UIImage variable and an NSString variable, but for some reason the UIImage variable is never retained even though I'm using properties. I've used this same code for singletons in other projects and it's always worked fine but it seems broken in iOS 5. And, by the way, I am NOT using ARC.</p> <p>Here is an example of what the header file looks like:</p> <pre><code>@interface MYSingleton : NSObject { NSString *aString; UIImage *anImage; } @property (nonatomic, retain) NSString *aString; @property (nonatomic, retain) UIImage *anImage; + (MYSingleton *) instance; @end </code></pre> <p>And here is the corresponding implementation file:</p> <pre><code>@implementation MYSingleton static MYSingleton *instance = nil; @synthesize aString, anImage; - (void) dealloc { [aString release]; [anImage release]; [super dealloc]; } + (MYSingleton *) instance { @synchronized(self) { if (instance == nil) { [[MYSingleton alloc] init]; // assignment not done here } } return instance; } + (id)allocWithZone:(NSZone *)zone { @synchronized(self) { if (instance == nil) { instance = [super allocWithZone:zone]; return instance; } } return nil; } - (id)copyWithZone:(NSZone *)zone { return self; } - (id)retain { return self; } - (unsigned)retainCount { return UINT_MAX; } - (void)release { // do nothing } - (id)autorelease { return self; } @end </code></pre> <p>By popular demand, here is how I assign a value to the UIImage variable:</p> <pre><code>- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { id image = [info objectForKey:UIImagePickerControllerOriginalImage]; MYSingleton *m = [MYSingleton instance]; m.anImage = image } </code></pre> <p>I also noticed a perhaps related difference in iOS 5 but with a category I created. This category was for UIDevice. I added a method that returned the platform as a string. I cach the string in a static variable I declared at the top of the category's implementation file. The first time the method was called the static variable is nil so the value is calculated and stored in the static variable. On subsequent calls to the method the cached variable is returned. This worked fine in iOS 4 but in 5 when ever this method is called more than once an EXEC_BAD_ACCESS error is thrown (except when I'm stepping through the code).</p> <p>Any help much appreciated.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload