0

I have a singleton class with this code:

manager.h

@interface Manager : NSObject {
  NSString *jobsLimit;
  NSMutableDictionary *jobTitles;
}

@property (nonatomic, retain) NSString *jobsLimit;
@property (nonatomic, assign) NSMutableDictionary *jobTitles;


@implementation Manager

@synthesize jobsLimit;
@synthesize jobTitles;

+ (id)sharedManager {
    @synchronized(self) {
        if(shared == nil)
            shared = [[super allocWithZone:NULL] init];
    }
    return shared;
}

- (id)init {
    if (self = [super init]) {  
        jobsLimit = [[NSString alloc] initWithString:@"50"];
        jobTitles = [[NSMutableDictionary alloc] init];
    }
    return self;
}

Then in the code i'm assigning these variables like this:

 self.jobsLimit = [NSString stringWithFormat:@"%d", progressAsInt];
 [self.jobTitles addEntriesFromDictionary:anotherDictionary];


- (void)dealloc {
    [super dealloc];
    [jobsLimit release];
    [jobTitles release];
}
  • Now my question is this code correct? Is the assignment correct?

  • I'm very confused with when to use alloc and/or retain. Do I need to use alloc if my property is already retained? and If I use alloc should my property be assign?

  • What will be the reference count now for these variables and will they be dealloc'd/under-dealloc'd when the dealloc is called?

  • Also for singleton classes do I need to initialize my ivars as in the init method above or I do not have to.

I'd appreciate if someone can help me clear this confusion out and thanks in advance.

Regards,

Cezar
  • 55,636
  • 19
  • 86
  • 87
mshaaban
  • 211
  • 1
  • 3
  • 12

2 Answers2

2

I guess you need a lot of this : IOS Memory Management

and a bit of : What should my Objective-C singleton look like?

good lectures !

Community
  • 1
  • 1
VdesmedT
  • 9,037
  • 3
  • 34
  • 50
2

Your code actually looks correct, but perhaps some explanation is in order, since it sounds like you're a little unsure.

When you assign to a property that has retain semantics using the "." syntax, the accessor method calls retain. The "." syntax is just shorthand for invoking the accessor method, so

self.jobsLimit = [NSString stringWithFormat:@"%d", progressAsInt];

is exactly the same as

[self setJobsLimit:[NSString stringWithFormat:@"%d", progressAsInt]];

That works out to:

  • create an (autoreleased) string with a numeric value
  • retain the string (you now own it) and assign it to jobsLimit

If, on the other hand, you assign to the iVar directly (not using the "."-accessor), the setter method is not called. For example:

jobsLimit = [[NSString alloc] initWithString:@"50"];

That is:

  • allocate a string (you own it), with value "50"
  • assign it to jobsLimit

Either way, you now own the string referred to by jobsLimit, and are responsible for eventually releasing it (e.g., in your dealloc method).

David Gelhar
  • 27,873
  • 3
  • 67
  • 84
  • Thanks a lot David, but now the alloc'd string in "[[NSString alloc] .." will it ever gets dealloc'd since now jobsLimit is pointing to another string "[NString stringWith ..."??? – mshaaban Dec 21 '10 at 18:55
  • As long as you use the accessor method (self.jobsLimit = ...) to assign, you'll be ok: the accessor method calls `release` on the old value before assigning the new one. – David Gelhar Dec 22 '10 at 05:39