• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Calling a Cocoa library from C
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Calling a Cocoa library from C


  • Subject: Re: Calling a Cocoa library from C
  • From: Jean-Daniel Dupas <email@hidden>
  • Date: Sat, 12 Nov 2011 11:29:12 +0100

Le 12 nov. 2011 à 03:34, Charles Srstka a écrit :

> On Nov 11, 2011, at 8:22 PM, Wim Lewis wrote:
>
>> On Nov 11, 2011, at 5:49 PM, Nathan Sims wrote:
>>> Newb question. I need to create an OS X Cocoa library that is going to be called from a C program. The C program's interface will be simple, along the lines of:
>>>
>>> retval=get_float_data(&float1,&float2);
>>>
>>> where get_float_data() is a function that resides in the Cocoa library and invokes Objc code to generate and return two float values to the caller.
>>>
>>> I'm unclear on how to architect this. Can a C function invoke Objc methods?
>>> (OSX 10.6.8, Xcode 3.2.6)
>>
>> Yes. There's no real barrier between the two (even less of a barrier than there is between C and C++). Everything works as you'd expect.
>>
>> Unless your calling program is being invoked from other ObjC code (like an application's event loop), you may want to put an autorelease pool and an exception-catching block in the called C function. The autorelease pool will make sure that temporary objects created in the ObjC code get deallocated when get_float_data() returns, and although exceptions can propagate through C functions you probably want to convert them into an error code or exit(1)  or something.
>>
>> int get_float_data(float *result1, float *result2)
>> {
>> NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
>> @try {
>>  [objcCode call];
>>  *result1 = [more stuff];
>>  etc.;
>> } @catch {
>>  fprintf(stderr, "omg doomed!\n");
>>  etc.;
>> } @finally {
>>  [pool drain];
>> }
>>
>> return blahblah;
>> }
>
> In this day and age, you should probably just use @autoreleasepool instead of NSAutoreleasePool:
>
> int get_float_data(float *result1, float *result2)
> {
> 	@autoreleasepool {
> 		[objcCode call];
> 		*result1 = [more stuff];
> 		etc.;
> 	}
>
> 	return blahblah;
> }


Note that this code is not equivalent with the previous one. @autoreleasepool does not drain on exception.

-- Jean-Daniel




_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: Calling a Cocoa library from C
      • From: Greg Parker <email@hidden>
References: 
 >Calling a Cocoa library from C (From: Nathan Sims <email@hidden>)
 >Re: Calling a Cocoa library from C (From: Wim Lewis <email@hidden>)
 >Re: Calling a Cocoa library from C (From: Charles Srstka <email@hidden>)

  • Prev by Date: Re: How to determine if there is an internet connection available?
  • Next by Date: Re: capturing self is likely to lead to a retain cycle ..
  • Previous by thread: Re: Calling a Cocoa library from C
  • Next by thread: Re: Calling a Cocoa library from C
  • Index(es):
    • Date
    • Thread