Analyzer marks a potential leak for this construct

With the following code the analyzer marks the setMyDict selector call as a potential leak and in dealloc it states "Incorrect decrement of the reference count is not owned at this point by the caller"

- (id)init {
  if (self = [super init]) {
      [self setMyDict:[[NSMutableDictionary alloc] init]];
  }
  return self;
}

- (void)dealloc {
  [[self myDict] release];
  [super dealloc];
}

@synthesize myDict = _myDict;

I do not understand this. I thought, that with the alloc init the object increases the retain count by one and the pointer is stored in _myDict through the synthesized property. If I use this code instead

- (id)init {
  if (self = [super init]) {
    _myDict = [[NSMutableDictionary alloc] init];
  }
  return self;
}

- (void)dealloc {
  [_myDict release];
  [super dealloc];
}

Analyzer does not complain. What am I missing?

Answers


The @synthesize provides you with a setter and getter for the object you're synthesizing.

A setter method looks something like this (taken from the Apple docs)

-(void)setMyDict:(NSMutableDictionary *)newDict {
    if (myDict != newDict) {
       [myDict release];
       myDict = [newDict retain];
    }
}

You're creating a leak when you do:

[self setMyDict:[[NSMutableDictionary alloc] init]];

Because you never release the newly alloc'd dictionary.

A way to work around this is:

NSMutableDictionary * dict = [[NSMutableDictionary alloc] init];
[self setMyDict:dict];
[dict release];

This takes care of the leak.

In the dealloc method, you should use:

[myDict release]; // Or whatever your property is called.

Need Your Help

How to the Trello API Batch method

trello

I am learning the Trello API and for the most part it is not difficult. However, the batch method, for batching GET requests has real potential to minimize traffic and so on. However, I can't get i...

Include table column background colors when exporting to .xls/.pdf

jquery html5 datatables export-to-excel export-to-pdf

I'm using jQuery DataTables to show a table that has multiple cell colors based on employee availability in certain months. I have included 'Buttons' extension of DataTables to it so that the table...