0

I have memory leak on jsonParser.

Here is my code

- (id) objectWithUrl:(NSURL *)url {
SBJsonParser *jsonParser = [SBJsonParser new];
NSString *jsonString = [self stringWithUrl:url];

// Parse the JSON into an Object
return [jsonParser objectWithString:jsonString error:nil]; }

This is the error message I'm getting, potential leak of an object allocated on line 192 and stored into 'jsonParser'

Please help.

HardCode
  • 99
  • 2
  • 8

1 Answers1

4

+new is equivalent to the [[SBJsonParser alloc] init] call so you're responsible to release jsonParser object. As you use it in return statement the easiest way to fix leak will be to autorelease it right after creating:

SBJsonParser *jsonParser = [[SBJsonParser new] autorelease];
Vladimir
  • 170,431
  • 36
  • 387
  • 313
  • In addition, you should consider sending `alloc` and `init` messages to classes instead of `new`, while these are functionally equivalent, technically `new` is not part of Objective-C and is not what is widely used by the community. [Here are a few more reasons to use `alloc` and `init` instead of `new`.](http://macresearch.org/difference-between-alloc-init-and-new) – Beltalowda Sep 07 '11 at 15:46
  • 3
    @Thuggish: Technically, `alloc` and `init` are not part of Objective-C. They are part of the Foundation framework. In fact `new` predates them and actually derives from Smalltalk. Anyway, your point stands. Everybody uses `alloc` `init`. `new` looks a bit odd in the context of Objective-C. – JeremyP Sep 07 '11 at 16:02
  • You'll likely see `new` used more and more often in the context of ARC, it seems. – bbum Sep 07 '11 at 16:13