ios - Objective-C: Potential leak of an object stored into NSDictionary -



ios - Objective-C: Potential leak of an object stored into NSDictionary -

i newbie objective-c. xcode highlights next code issue. _mycookies private variable of class store received cookies.

@implementation messagesender { nsarray *_mycookies; } ... // socket open callback - (void) gotdata:(mysocket *) socket { nsdictionary *responseheaders = (__bridge nsdictionary*)cfhttpmessagecopyallheaderfields(socket.httpheaders); _mycookies = [nshttpcookie cookieswithresponseheaderfields:responseheaders forurl:[nsurl urlwithstring:@""]]; }

the highlighted issues are:

call function 'cfhttpmessagecopyallheaderfields' returns core foundation object +1 retain count object leaked: object allocated , stored 'responseheaders' not referenced later in execution path , has retain count of +1

how resolve one? using arc. want store cookies in class can later utilize them while sending request, want allow socket take care of responseheaders.

you need transfer ownership of corefoundation object arc:

nsdictionary *responseheaders = cfbridgingrelease(cfhttpmessagecopyallheaderfields(socket.httpheaders));

cf objects not managed arc, , must released manually. when utilize __bridge cast, you're casting object straight no alter in ownership. causes memory leak (you need manually release cf objects cfrelease when you're done them).

you can avoid manual memory management problem using cfbridgingrelease cast object (as in code illustration above). transfers ownership of cf object arc, arc automatically handle releasing object you, , work fine no work on part.

ios objective-c

Comments

Popular posts from this blog

php - How to pass multiple values from url -

xslt - DocBook 5 to PDF transform failing with error: "fo:flow" is missing child elements. Required content model: marker* -

database - php search bar when I press submit with nothing in the search bar it shows all the data -