views:

57

answers:

3

I have a a singleton class here is the code

#import <Foundation/Foundation.h>
#import "CustomColor.h"


@interface Properties : NSObject {
    UIColor *bgColor;
    CustomColor *bggColor;
}
@property(retain) UIColor *bgColor;
@property (retain) CustomColor *bggColor;

+ (id)sharedProperties;
@end

#import "Properties.h"

static Properties *sharedMyProperties = nil;

@implementation Properties

@synthesize bgColor;
@synthesize bggColor;

#pragma mark Singleton Methods
+ (id)sharedProperties
{
    @synchronized(self)
    {
            if(sharedMyProperties == nil)
            [[self alloc] init];
    }
    return sharedMyProperties;
}
+ (id)allocWithZone:(NSZone *)zone
{
    @synchronized(self)
    {
            if(sharedMyProperties == nil)
            {
                sharedMyProperties = [super allocWithZone:zone];
                return sharedMyProperties;
            }
    }
    return nil;
}
- (id)copyWithZone:(NSZone *)zone
{
  return self;
}
- (id)retain {
    return self;
}
- (unsigned)retainCount {
    return UINT_MAX; //denotes an object that cannot be released
}
- (void)release {
    // never release
}
- (id)autorelease {
    return self;
}
- (id)init {
    if (self = [super init])
    {
        bgColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:1.0];

        FSColor *bc = [[FSColor alloc] init];
        bc.red = bc.green = bc.blue = bc.hue = bc.sat = bc.bri = 0;
        bggColor = bc;

    }
    return self;
}
- (void)dealloc
{
    // Should never be called, but just here for clarity really.
    [bgColor release];
    [bggColor release];
    [super dealloc];
}
@end

I have a UIView' subclass. in which i am using it. I am calling drawRect method after each second. It only run once and then app crashes.

- (void)drawRect:(CGRect)rect 
{
    Properties *sharedProprties = [Properties sharedProperties];
    …...
    ….
    CGContextSetFillColorWithColor(context, [[sharedProprties bgColor] CGColor]);
    …..
}
+2  A: 

What if you do self.bgColor = [UIColor colorWithRed:...]?

Without the self. I think you may be accessing the ivar directly, and therefore assigning it an autoreleased object which won't live very long (rather than using the synthesized property setter, which would retain it). I could be wrong about this, I'm stuck targeting older Mac OS X systems so I haven't been able to play much with Objective-C 2.0.

dreamlax
A: 

Your static sharedProperties gets never assigned. It is missing in the init method or in the sharedProperties static method.

Here's a sample pattern for singletons (last post)

Also accessing properties without self. may cause bad access errors too.

Regards

ReaperXmac
It is assigned inside `+allocWithZone:`
dreamlax
Yes, but as far as i know the assignment is done in the allocWithZone method, the instance could be stored on a different memory location (different memory zone) as the variables do which can cause access errors. So I would assign the sharedInstance in the init method instead of allocWithZone
ReaperXmac
The space for an instance's ivars is allocated wherever the instance is allocated.
dreamlax
A: 

Not an answer, but note that a simple call to [Properties alloc] will mean it's no longer a singleton!

+ (id)sharedProperties
{
  @synchronized(self)
  {
    if(sharedMyProperties == nil)
    {
      sharedMyProperties = [[self alloc] init];
    }
  }
  return sharedMyProperties;
}
tc.