简体   繁体   中英

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!!

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 .

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM