objective c - Garbage Collector and Core Foundation -
i wrote method load image calayer. code:
- (cgimageref)loadimage:(nsstring*)path { // data image cgimageref image = null; nsdata *data = [nsdata datawithcontentsoffile:path]; cfdataref imgdata = (cfdataref)data; cgdataproviderref imgdataprovider = cgdataprovidercreatewithcfdata (imgdata); // cgimage cfdataref image = cgimagecreatewithjpegdataprovider(imgdataprovider, null, true, kcgrenderingintentdefault); // if image isn't jpg image, png file if (!image) image = cgimagecreatewithpngdataprovider(imgdataprovider, null, true, kcgrenderingintentdefault); return image; }
i use method in calayer:
nsstring *pathstring = // image path; alayer = [calayer layer]; alayer.contents = [self loadimage:pathstring];
it's work. finalize view (using garbage collector) application has leaks. should release cfdataref imgdata? read garbage collector not work in core foundation.
, excuse english.
you responsible releasing object calling cgimagerelease.
see documentation on garbage collection:
by default, therefore, in garbage-collected environment manage core foundation objects in reference-counted environment (as described in memory management programming guide core foundation > “ownership policy”). if create or copy core foundation object, must subsequently release when you’re finished it. if want keep hold of core foundation object, must retain , again subsequently release when you’re finished it.
Comments
Post a Comment