简体   繁体   中英

Retrieving the retain count for a CGImageRef object?

While you can release a reference to a CGImageRef object using "CGImageRelease", which, according to the SDK this "decrements the retain count of a bitmap image", is there a way to inspect the current retain count for a CGImageRef instance? [cgImageRef retainCount] isn't valid since CGImageRef isn't a valid receiver of the retainCount message.

In other words, during dealloc within a class that renders an EAGLContext, I want to make sure any outstanding references to CGImageRef objects are released but I obviously don't want to call CGImageRelease(someCGImageRef) if its retain count is already 0. I've found in practice that just checking to see if the image ref is nil isn't consistent with current retain count values.

Is it a best practice to simply set the CGImageRef instance to nil after you're done with it and you've already released it so that a check for (someCGImageRef == nil) lets you know if there is an outstanding reference to it?

Thanks

If I read the docs correctly, CGImageRelease is like CFRelease except that it handles NULL differently. That means CFGetRetainCount should work as long as cgImageRef is not NULL.

Okay, so I read the rest of your question after that. You shouldn't be calling CFGetRetainCount on an object that may already have a retainCount of 0, since by then the object is already destroyed. Instead, set it to NULL.

(NULL and nil are equivalent, but nil is for Objective-C objects. CGImageRef isn't one of those, so you should probably use NULL to avoid confusing yourself later.)

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