Re: Wrapping a C function library (as source) in an ObjC class...
Re: Wrapping a C function library (as source) in an ObjC class...
- Subject: Re: Wrapping a C function library (as source) in an ObjC class...
- From: Jens Alfke <email@hidden>
- Date: Sun, 28 Mar 2010 12:28:37 -0700
On Mar 28, 2010, at 9:32 AM, Clark Cox wrote:
>> When importing header files into my
>> implementation file ".m", do I #include them or #import them? How about
>> existing library headers (like <string.h>)?
>
> You can do either.
To be precise: #import is just like #include, except that the second #import of the same path does nothing. So in almost any case it’s safe to #import regular C headers, since it’s a very rare header that’s meant to be imported multiple times.
>
>> 2) If my C code uses malloc/free, and my ObjC project has garbage collection
>> turned on, what 'gotcha's do I have to watch out for? What if GC is turned
>> off (for iPhone)?
>
> The Garbage collector completely ignores memory allocated via malloc.
> Regardless of whether or not GC is on, you are still responsible for
> free()'ing malloc()'ed memory.
The main requirement here is that a class that causes memory to be malloc’ed has to have a -finalize method that frees that memory. (Very often the -finalize will be identical to the -dealloc.) This applies whether you called malloc directly, or some C/C++ library [including CF] that allocated memory for you. In the latter case of course you have to issue the corresponding clean-up calls.
—Jens
_______________________________________________
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