Sunday, 15 January 2012

ios - Xcode static analyser complaining about potential leak when using ARC -



ios - Xcode static analyser complaining about potential leak when using ARC -

i using arc ios sdk 6.0.

i pretty sure have got memory leaks im having problem tracking down.

after running static analyser, im getting warnings next 2 methods:

+ (id<mxurlrequest>) requestwithurl:(nsurl*)url { mxasiurlrequest *request = [[mxasiurlrequest alloc] init]; [request seturl:url]; homecoming request; // static analyser: potential leak of object stored 'request' } - (id)parsebody:(nserror *)error { nsstring *contenttype = [[_request responseheaders] objectforkey:@"content-type"]; id body = nil; if ([contenttype hasprefix:@"application/json"] || [contenttype hasprefix:@"text/json"] || [contenttype hasprefix:@"application/javascript"] || [contenttype hasprefix:@"text/javascript"]) { body = [nsjsonserialization jsonobjectwithdata:[_request responsedata] options:nsjsonreadingmutableleaves error:&error]; } else if ([contenttype hasprefix:@"image/"] || [contenttype hasprefix:@"audio/"] || [contenttype hasprefix:@"application/octet-stream"]) { body = [_request responsedata]; } else { body = [[nsstring alloc] initwithdata:[_request responsedata] encoding:nsutf8stringencoding]; } homecoming body; // static analyser : potential leak of object stored 'body' }

the warnings follows...

object leaked: object allocated , stored 'request' returned method name ('requestwithurl:') not start 'copy', 'mutablecopy', 'alloc' or 'new'. violates naming convention rules given in memory management guide cocoa object leaked: object allocated , stored 'body' returned method name ('parsebody:') not start 'copy', 'mutablecopy', 'alloc' or 'new'. violates naming convention rules given in memory management guide cocoa

can see i've done wrong here? these warnings legitimate, or can ignored? me these methods valid arc able handle automatic reference counting.

any help much appreciated.

arc turned off either file compiling downwards to, or on whole project. static analyzer complains stuff when arc turned off, i'll elaborate farther in mind.

the cocoa memory management guidelines compiler references allow strict set of methods homecoming non-autoreleased objects constructors under normal circumstances (those beingness 'init', 'copy', 'mutablecopy', , 'new'). notice pattern?

because allocating object in convenience constructor, handing caller, 1 owns it, because created it. cocoa wants append autorelease end of homecoming caller's job maintain reference newly constructed object:

+ (id<mxurlrequest>) requestwithurl:(nsurl*)url { mxasiurlrequest *request = [[mxasiurlrequest alloc] init]; +1, own [request seturl:url]; homecoming [request autorelease]; // +0, it's caller's problem }

as lastly method, cocoa complaining beingness inconsistent memory-wise. method has possibility of returning either object +1, or +0 retain count because of mixed utilize of constructors , conveniences. illustrate:

- (id)parsebody:(nserror *)error { nsstring *contenttype = [[_request responseheaders] objectforkey:@"content-type"]; id body = nil; if ([contenttype hasprefix:@"application/json"] || [contenttype hasprefix:@"text/json"] || [contenttype hasprefix:@"application/javascript"] || [contenttype hasprefix:@"text/javascript"]) { body = [nsjsonserialization jsonobjectwithdata:[_request responsedata] options:nsjsonreadingmutableleaves error:&error]; //returns +0 } else if ([contenttype hasprefix:@"image/"] || [contenttype hasprefix:@"audio/"] || [contenttype hasprefix:@"application/octet-stream"]) { body = [_request responsedata]; //potentially returns +1 } else { body = [[nsstring alloc] initwithdata:[_request responsedata] encoding:nsutf8stringencoding]; //returns +1 } homecoming body; // static analyser : potential leak of object stored 'body' }

the analyzer asking consistency: either utilize constructors, or autorelease consistently.

ios objective-c memory-management automatic-ref-counting

No comments:

Post a Comment