CFRetain a C string with garbage collector enabled
I was reading this http://lists.apple.com/archives/objc-language/2011/Mar/msg00084.html
This is a long (interesting) thread, 开发者_StackOverflowand i may well have missed the point...
This bit caught my eye
char* path = [string fileSystemRepresentation];
CFRetain (path);
int result = open (path, ...);
CFRelease (path);
I know that you can't do this when not garbage collected (char* is not a CFType). Does this do anything when garbage collection is enabled?
My thought is that this is a mistake, or it is not actually being proposed as a solution, although this is how i read it.
Yes, it's a mistake; CFRetain only works on CFTypes. C pointers/structures are not garbage collected, even with GC enabled. In non-GC, the string is placed in an autorelease pool, so you don't need to worry about it until the pool is drained. If you do need to hold onto the string, then you need to make a copy of it.
You might find the section of the docs on interior pointers useful; the char *
you're getting back is essentially an interior pointer though you don't have access to its containing object.
精彩评论