Re: Cocoa-Carbon Bridging
Re: Cocoa-Carbon Bridging
- Subject: Re: Cocoa-Carbon Bridging
- From: John Stiles <email@hidden>
- Date: Mon, 25 Jul 2005 18:27:57 -0700
On Jul 25, 2005, at 5:58 PM, Zameer Andani wrote:
Hi, i'm new to cocoa-carbon bridging. I was wondering if someone
could help.
I have the following class:
class Foo {
CFStringRef name;
~Foo() { CFRelease(name); }
}
and name has the value of "Bob" at location 0x309278.
Apparantely i should be able to do the following in Cocoa:
Foo *foo = new Foo();
.... foo->name is inititialized here
...
NSString *cocoaString = (NSString *)(foo->name);
delete(foo)
when the delete completes, cocoaString becomes invalid. I have
tried everything to produce a copy: CFCreateWithCopy(), assignment,
even the following:
NSString *cocoaString2 = [NSString stringWithUTF8: [cocoaString
UTF8String]];
in all cases, all my strings become invalid upon delete. Anyone
have any ideas on how I can actually produce a copy of a CFStringRef?
cocoaString2 is a completely new string. There is no way that
CFRelease on your object can be causing cocoaString2 to be released.
OTOH, you are not retaining cocoaString2, so it will be deleted the
next time you go through the event loop.
I can't explain why CFCreateWithCopy wouldn't work, unless there was
a separate bug in your code (definitely something which you shouldn't
rule out).
Assuming there are no other bugs which are causing weird failures, I
think the easiest solution for you would be:
NSString *cocoaString = [((NSString *)(foo->name)) copy];
This will produce a unique copy of the string for you to use.
(Technically, if it is an immutable string, you will just bump the
reference count on the original string, but it will work either way.)
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden