Yaniv Efraim
Yaniv Efraim

Reputation: 6713

objective-c memory leak + NSMutableDictionary

I am using a dictionary with parameters to send to a web service. I keep getting memory leaks from it, altho I do a release for the object. My code:

 NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];

// getting an NSString
NSString *clientID = [prefs stringForKey:@"ClientID"];



//Call web service
id delegate1 = self;
AsyncWebServiceController * webService = [[[AsyncWebServiceController alloc] initWithDelegate:delegate1
                                                                                 selSucceeded:@selector(webServiceConnectionSucceeded:)
                                                                                    selFailed:@selector(webServiceconnectionFailed:)] autorelease];


NSMutableDictionary *params = [[NSMutableDictionary alloc] init]; //Memory leak here
[params setValue:clientID forKey:@"clientId"]; //Memory leak here
[params setValue:[self.jobDetails objectForKey:@"BoardId"] forKey:@"jobBoardId"];
[params setValue:[self.jobDetails objectForKey:@"Id"] forKey:@"jobId"];

[webService startRequest:@"JobReviewGet" parameters:params];

[params release];

Any ideas? Thanks!!

Upvotes: 0

Views: 1607

Answers (4)

sElanthiraiyan
sElanthiraiyan

Reputation: 6268

Why don't you use the convenience constructor?

NSMutableDictionary *params = [NSMutableDictionary dictionary]; 

Then there would be no need to release it. This should solve your problem.

Upvotes: 2

rckoenes
rckoenes

Reputation: 69469

You should be using :

- (void)setObject:(id)anObject forKey:(id)aKey;

Thus you code should look like:"

NSMutableDictionary *params = 
[[NSMutableDictionary alloc] init]; //Memory leak here
[params setObject:clientID forKey:@"clientId"]; //Memory leak here
[params setObject:[self.jobDetails objectForKey:@"BoardId"] forKey:@"jobBoardId"];
[params setObject:[self.jobDetails objectForKey:@"Id"] forKey:@"jobId"];

[webService startRequest:@"JobReviewGet" parameters:params];

[params release];

What does [webService startRequest:@"JobReviewGet" parameters:params]; method do with the param, does it retain it? If so you also need to release it there.

Also ar some point you will need to release the webService.

Upvotes: 2

bryanmac
bryanmac

Reputation: 39296

Build with analyze. That may give some pointers.

Outside of that:

  1. You are calling alloc on AsyncWebServiceController do you own it but the code above does not release.
  2. You should be calling setObject:forKey instead of setValue:ForKey. setValue is for KVC.

Upvotes: 2

jbat100
jbat100

Reputation: 16827

Are you releasing your webService object at some point? Also is the delegate of AsyncWebServiceController an assigned or retained property? Delegates should generally be assigned properties to avoid retain loops.

Upvotes: 1

Related Questions