Tuesday, 15 July 2014

objective c - Proper memory management with CFStringRef and CFRelease -



objective c - Proper memory management with CFStringRef and CFRelease -

consider simple method; it's category on nsstring.

- (nsstring *)stringbyurlencoding { cfstringref newstring = cfurlcreatestringbyaddingpercentescapes(null, (cfstringref)self, null, (cfstringref)@"!*'\"();:@&=+$,/?%#[]% ", kcfstringencodingutf8); nsstring *returnstring = (__bridge nsstring *)newstring; cfrelease(newstring); homecoming returnstring; }

its job turn = %3d, etc. url encoding, that's not relevant question.

a few questions memory management:

after cfrelease(newstring); called, can still po newstring in debugger , see string. mean it's not getting deallocated properly?

when pass casted cfstringrefs arguments of cfurlcreatestringbyaddingpercentescapes, (like (cfstringref)self, example) believe not need store variable , cfrelease them due "toll-free bridging". correct?

does reply #2 alter if it's cgimageref, not cfstringref? why cgimageref have own cgimagerelease function, there's no cfstringrelease method?

to create sure understanding of __bridge_transfer correct, modified code identical?

- (nsstring *)stringbyurlencoding { cfstringref newstring = cfurlcreatestringbyaddingpercentescapes(null, (cfstringref)self, null, (cfstringref)@"!*'\"();:@&=+$,/?%#[]% ", kcfstringencodingutf8); nsstring *returnstring = (__bridge_transfer nsstring *)newstring; // cfrelease(newstring); homecoming returnstring; }

as questions:

1) don't have plenty info know if newstring should have been deallocated. instance, if no character replacement needed, reasonable cfurlcreatestringbyaddingpercentescapes homecoming equivalent of [self retain]; 1 example. broader point "you can't know." normal approach here utilize instruments heap growth , leaks. more information, google "retaincount useless" (i won't rehash here.)

2) yes.

3) cgimageref not toll-free bridged, however, understand it, cfretain , cfrelease should work. difference between cfrelease , cgimagerelease here parameter cfrelease must not null, whereas cgimagerelease tolerate null parameters.

4) __bridge_transfer appropriate annotation utilize here. "transfers" +1 retain count side arc doesn't handle side handle. set differently, __bridge_transfer tells arc need create release phone call pointer, through never generated retain.

objective-c automatic-ref-counting core-foundation

No comments:

Post a Comment