Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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, and 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.

like image 536
hooleyhoop Avatar asked Feb 21 '26 08:02

hooleyhoop


1 Answers

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.

like image 80
Nicholas Riley Avatar answered Feb 23 '26 06:02

Nicholas Riley



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!